Python Mysql Cursor.Execute语法

时间:2015-02-17 08:13:15

标签: python mysql cursor

我一直在寻找正确的方法来执行我的cursor.execute行,但没有找到适合我的方法。这是我的代码:

sa = 12345
alert_text = "Help me"
logtype = "Event"
cursor.execute("""INSERT INTO logs(customer_id, log_type, log_entry) VALUES (%s, %s, %s)""", (sa,logtype,alert_text,))

我已经尝试了我找到的每个方法(除了正确的方法)并从#34中得到各种错误;没有足够的参数"到mysql语法错误。

如您所见,sa,logtype和alert_text是变量,而不是必须插入的实际文本。我只是简单地为上面的每个变量添加了定义,因为每次变量都可以是不同的。

确切错误:

Traceback (most recent call last):
  File "listen.py", line 57, in <module>
    cursor.execute("""INSERT INTO logs(customer_id, log_type, log_entry) VALUES (%s, %s, %s)""", (sa,logtype,alert_text,))
  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 '))' at line 1")

0 个答案:

没有答案