这似乎是编程中的一个典型问题,但我在谷歌中没有发现任何有用的东西。这是代码:
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)
顺便修一下