在Python MySQLdb中转义单引号
这看起来是编程中常见的问题,但我在谷歌上没有找到什么有用的资料。
这是我的代码:
file=open('list.txt','r')
for line in file:
cursor.execute("CREATE TABLE IF NOT EXISTS \
%s(id INT(2) NOT NULL PRIMARY KEY AUTO_INCREMENT, \
entry TEXT NOT NULL)" % line)
file.close()
cursor.close()
db.close()
这是我在尝试用单引号创建表时遇到的错误:
Traceback (most recent call last):
File "test.py", line 104, in <module>
entry TEXT NOT NULL)" % line)
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.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 ''Hi 'Buddy!(id INT(2) NOT NULL PRIMARY KEY AUTO_INCREMENT, entry ' at line 1")
顺便说一下,MySQLdb.escape_string(line) 并没有解决这个问题。
5 个回答
0
与其使用 % 格式来插入值,不如把你的语句直接传给 cursor.execute()
,在需要替换的每个值的位置用 %s
代替,然后把所有要替换的值放在一个列表或元组里作为第二个参数,比如:
stmt = "CREATE TABLE IF NOT EXISTS %s(id INT(2) NOT NULL PRIMARY KEY AUTO_INCREMENT, entry TEXT NOT NULL)"
value_list = [line]
cursor.execute(stmt, value_list)
这样做会自动处理所有必要的转义,因为它会把你的字符串变成一个有效的 SQL 字面量。这段内容直接来自于 MySQLdb 用户指南,下面是相关的例子:
要执行查询,首先需要一个游标,然后你就可以在这个游标上执行查询:
c=db.cursor() max_price=5 c.execute("""SELECT spam, eggs, sausage FROM breakfast WHERE price < %s""", (max_price,))
在这个例子中,max_price=5。那么,为什么在字符串中使用 %s 呢?因为 MySQLdb 会把它转换成一个 SQL 字面量值,也就是字符串 '5'。当它完成后,查询实际上会变成 "...WHERE price < 5"。
1
据我所知(可以查看这里的MySQL 5.0参考资料),在表名中是不允许使用单引号的:
未加引号的标识符中允许使用的字符:
ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore) Extended: U+0080 .. U+FFFF
0
我不太明白你为什么要这么做,我觉得你可以考虑重新设计一下你的数据库结构。不过,你可以用反引号 `
来转义表名。
# notice the `'s surrounding %s
for line in file:
cursor.execute("CREATE TABLE IF NOT EXISTS \
`%s`(id INT(2) NOT NULL PRIMARY KEY AUTO_INCREMENT, \
entry TEXT NOT NULL)" % line)