因此,我正在运行一个Python脚本,该脚本在我的MySQL数据库中创建了一些表,并且一直收到以下错误:
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")问题是,它只在我尝试创建名为0e7cc62d5339491aa701b67453405ccb的表时给出错误,除此之外,脚本运行得很好!
以下是发生错误的区域的代码:
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 />"
print 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,))发布于 2011-01-05 08:15:42
问题出在以0e7开头的表名0e7cc62d5339491aa701b67453405ccb中。用反引号(`)对表名进行转义,它应该可以正常工作。然而,正如@Randy所说的那样,这种命名方案不是一个好主意。除非你有很好的理由,否则我会认真考虑改进它。
引用the docs
建议您不要使用以Me或MeN开头的名称,其中M和N是整数。例如,避免使用1e作为标识符,因为诸如1e+3之类的表达式是不明确的。根据上下文,它可能被解释为表达式1e +3或数字1e+3。
https://stackoverflow.com/questions/4599739
复制相似问题