-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 に答える