-1

我正在尝试使用 Python 和 MySQLdb 将一些数据插入 MySQL DB。当我执行以下操作时:

query = "INSERT INTO universitats (universitat) VALUES ('%s')" % (lloc)
cursor.execute(query)
db.commit()

我收到此错误:

Traceback (most recent call last):
  File "read.py", line 39, in <module>
    cursor.execute(query)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, 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 'Hospitalet de Llobregat')' at line 1")

我究竟做错了什么?

4

1 回答 1

1

这行:

 query = "INSERT INTO universitats (universitat) VALUES ('%s')" % (lloc)
 cursor.execute(query)

应该是这样的

query = "INSERT INTO universitats (universitat) VALUES (%s)"  
cursor.execute(query,(lloc,))

然后提交。

于 2013-07-18T22:09:28.510 回答