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))
注释掉的 cursor.execute
有效,未注释的抛出错误:
_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 'group, capacity, level, mail_id) VALUES ('NULL', '2014-12-05 23:46:56', 'succeed' at line 1")
“id
”列有 AUTO_INCREMENT
为什么会出现此错误?
最佳答案
group
是一个 reserved keyword in MySQL .使用反引号转义名称
INSERT INTO {0} (id, timestamp, status, priority, client, `group`, capacity ...
here--------------------------------^-----^
或者更好的是使用不同的列名。
并且不要使用 'NULL'
作为参数。使用不带引号的 NULL
或将其从查询中完全删除:
INSERT INTO {0} (timestamp, ...
^----------no ID
关于mysql - _mysql_exceptions.ProgrammingError : (1064, “你的 SQL 语法有错误;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52917242/