Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two table similar. The only difference is the name. One name is 192.168.1.7
and orther is tt_control
I write code read data from table, here is my code:
Python
import MySQLdb
db_local = MySQLdb.connect("localhost","root","root","luan_van")
num = 41001662
with db_local:
     cur = db_local(MySQLdb.cursors.DictCursor)
     cur.execute("SELECT * FROM 192.168.1.7 WHERE Card_ID = '%d'" %num)
     rows = cur.fetchall()
     ID = 0
     for row in rows:
         ID = row['Card_ID']
         print ID

If I replace "192.168.1.7" in cur.execute("SELECT * FROM 192.168.1.7 WHERE Card_ID = '%d'" %num) with "tt_control". It work well. What wrong with it? How can I fix it?
Thanks for help.
Posted

Assuming 192.168.1.7 is your table name (and it looks a lot more like an IP address), try:
Python
cur.execute("SELECT * FROM [192.168.1.7] WHERE Card_ID = '%d'" %num)
 
Share this answer
 
Comments
Member 10390715 13-Nov-14 11:40am    
I try with your comment but it is still error.
You must escape the name. With MySQL, the escape character is a weak accent: `, i.e. `192.168.1.7`.
Apart from that, your database seems to be wrongly designed. Why do you create a table with the name of an IP address, and have another table with the same columns? Think of normalization!
 
Share this answer
 
Comments
Member 10390715 14-Nov-14 2:33am    
Thanks.Because, first I make a table with name "192.168.1.7" and write code, I have error near the name "192.168.1.7". Then I make the same table with name "tt_control" and use this code to check what wrong, It work well.It makes me hard to understand!
Bernhard Hiller 14-Nov-14 2:41am    
Not all combinations of digits and characters are valid / acceptable names for tables or columns. Some inaccaptable names can still be used by escaping them (if the database engine allows that - e.g. Oracle does not). The rules differ between engines, but generally dots (.), commas, national characters, backslashes, and SQL keywords should be avoided.
Member 10390715 14-Nov-14 2:43am    
ok, I understand you. Thanks for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900