sql = ("INSERT INTO {0} "
"(id, timestamp, status, priority, client, group, capacity, level, mail_id) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)".format(TABLE_NAME_MAIL))
values = ('NULL', report['timestamp'], 'succeeded', report['priority'], c.strip(), report['group'], 'NULL', 'NULL', ref_mailid)
cursor.execute(sql, values)
#cursor.execute('INSERT INTO %s VALUES (NULL,"%s","%s","%s","%s","%s",NULL,NULL,"%s") ' % (TABLE_NAME_REPORT, report['timestamp'], 'succeeded', report['priority'], c.strip(), report['group'], ref_mailid))
Run Code Online (Sandbox Code Playgroud)
注释掉的cursor.execute工作,未注释的抛出错误:
_mysql_exceptions.ProgrammingError: (1064, "您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以获取在 'group, capacity, level, mail_id 附近使用的正确语法) VALUES ('NULL', ' 2014-12-05 23:46:56',在第 1 行'成功'")
列“ id”有AUTO_INCREMENT
为什么我收到这个错误?
group是MySQL 中的保留关键字。使用反引号来转义名称
INSERT INTO {0} (id, timestamp, status, priority, client, `group`, capacity ...
here--------------------------------^-----^
Run Code Online (Sandbox Code Playgroud)
甚至更好地使用不同的列名。
并且不要'NULL'用作参数。使用NULL不带引号或从查询中完全删除它:
INSERT INTO {0} (timestamp, ...
^----------no ID
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
599 次 |
| 最近记录: |