文章内容
2019/3/4 15:23:59,作 者: 黄兵
commands out of sync you can't run this command now python
最近在使用MySQLdb插入数据的时候,报如下错误:
commands out of sync you can't run this command now python
(2014, "Commands out of sync; you can't run this command now")
具体截图如下:

可以看到有人通过恶意提交,将一部分SQL语句注释掉了,本来一次执行完成的语句,分成两次执行。
不知道是否说的有问题,也可以参考一下这篇文章:MySQL-python: Commands out of sync
最后解决方案:
在调用该过程之后,我必须关闭游标并再次打开它,然后再使用它来执行另一个语句,具体代码如下:
with closing(Conn_Config().Conn_MySQL()) as conn_mysql:
with closing(conn_mysql.cursor()) as cur:
# SQL 插入语句
sql = "INSERT INTO visit_time_line_original(time, ip, url,user_agent) \
VALUES (%s, \'%s\', '\%s\', '\%s\')" % \
(item, get_ip, get_url, get_user_agent)
try:
# 执行sql语句
cur.execute(sql)
# 避免这个错误: Commands out of sync; you can't run this command now
cur.close()
# 提交到数据库执行
conn_mysql.commit()后面附了一些链接,可以更具体的了解这个问题。
参考链接:
1、MySQL-python: Commands out of sync
2、Python, “commands out of sync; you can't run this command now”
黄兵个人博客原创。
转载请注明出处:黄兵个人博客 - commands out of sync you can't run this command now python
评论列表