Error creating table named 0e7cc62d5339491aa701b67453405ccb in MySQL
So I'm running a Python script that's making some tables in my MySQL database, and I keep getting the following error:
File "build/bdist.linux-i686/egg/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "build/bdist.linux-i686/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near
'0e7cc62d5339491aa701b67453405ccb (\n\t email VARCHAR(50),\n\t price' at line 1")
The thing is, it only gives the error when I try to create the table named 0e7cc62d5339491aa701b67453405ccb
, other than that, the script runs fine!
Here's the code from the area that the error occurs:
def add(i, p, e):
conn = MySQLdb.connect(...)
cursor = conn.cursor()
e = str(e)
d = str(hashlib.md5(e).hexdigest())
i = str(i)
p = str(p)
q = """CREATE TABLE IF NOT EXISTS %s (
email VARCHAR(50),
price VARCHAR(15),
isbn VARCHAR(15) NOT NULL,
PRIMARY KEY (isbn))""" % (d,)
print e
print "<br />"
p开发者_JAVA技巧rint i
print "<br />"
print p
print "<br />"
print "<p />"
cursor.execute(q)
q = """REPLACE INTO %s (email,price,isbn)
VALUES (%%s,%%s,%%s)""" % (d,)
cursor.execute(q, (e,p,i,))
The problem lies in the table name 0e7cc62d5339491aa701b67453405ccb
starting with 0e7
. Escape the table name with backticks (`
) and it should work. However, as @Randy says this naming scheme is a bad idea. Unless you have very good reason, I'd seriously consider improving it.
Quoting the docs:
It is recommended that you do not use names that begin with Me or MeN, where M and N are integers. For example, avoid using 1e as an identifier, because an expression such as 1e+3 is ambiguous. Depending on context, it might be interpreted as the expression 1e + 3 or as the number 1e+3.
精彩评论