mysql - _mysql_exceptions.ProgrammingError : (1064, “你的 SQL 语法有错误;

标签 mysql python-3.6

        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/

相关文章:

python - 在子进程中重定向ffmpeg的输出

anaconda - OSError : Could not find lib geos_c. dll 或加载其任何变体 ['..\Library\\lib\\geos_c.dll']

mysql - sql group_concat失败

mysql - 我应该使用 ENUM 还是 TINYINT

python - 大数据数据透视表(2.5m 与 2m)

python - PyAutoGUI 在屏幕上定位,在 linux 上出现错误

python - 为什么使用 not() 运算符有时会出现语法错误?

opencv - 行,列,_ = frame.shape AttributeError: 'tuple'对象没有属性 'shape'

php - 使用 LIKE 语句执行时 PDO 的疯狂行为

c# - 使用 EF 使用 sum 进行慢速 mysql 查询