此命令成功将5个字段(所有字符串)添加到表的记录中:
import mysql.connector
...
add_scan = ("INSERT INTO tblScanpoint "
"(f1, f2, f3, f4, f5 ) "
"VALUES (%s, %s, %s, %s, %s)")
data_scan = ('AAAabc', 'AA', '4321', 'SEA', '3')
cursor = cnx.cursor()
cursor.execute(add_scan, data_scan)
cnx.commit()
...
第六个字段的数据类型是TIMESTAMP。
尝试更新所有六个字段时出错:
>>> cursor.execute(add_scan, data_scan)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'when ) VALUES ('CCCabc', 'AA', '4321', 'SEA', '3', '2016-11-07 20:46:56.35')' at line 1
修改了这些字符串以容纳第六个TIMESTAMP字段:
stamp = '2016-11-07 20:46:56.35'
add_scan = ("INSERT INTO scanpoint "
"(f1, f2, f3, f4, f5, when ) "
"VALUES (%s, %s, %s, %s, %s, %s)")
data_scan = ('CCCabc', 'AA', '4321', 'SEA', '3', stamp)
是否因为输入字符串而抛出错误? 寻求纠正措施建议。我们非常感谢聪明的例子。
答案 0 :(得分:1)
问题是when
您的列名是关键字/保留字。你需要使用backtique逃脱它。见下文
(f1, f2, f3, f4, f5, when )
^.... problematic column name
这应该被转义
(f1, f2, f3, f4, f5, `when` )