错误场景:
使用Python开发的数据库升级工具,在升级脚本行遇到如下语句报错了:
DROP TRIGGER IF EXISTS acl_2_ael_insert;
CREATE TRIGGER acl_2_ael_insert AFTER INSERT ON alarmclientlog
FOR EACH ROW
UPDATE alarmeventlog SET alarm_deal_type = new.alarm_deal_type
WHERE alarmeventlog.strIndex = new.strIndex
AND alarmeventlog.strUniqueTime = new.strUniqueTime;
先解释下这段语句,alarmeventlog
表示原始报警日志,alarmclientlog
表示报警处理日志。当alarmclientlog
插入或更新后,要把处理结果也同步到alarmeventlog
里面去。
因此,我创建一个触发器做同步操作,为防止之前已经有此触发器,我先检查并删除下。
问题定位:
在网上搜到如下有用的资料:
Python’s MySQLdb 2014 Error – Commands out of sync
这里面说可能跟多条语句放在一起执行有关。
但是有些语句是可以放在一起执行的,那是不是对SQL语句的类型由要求?
然后我找到了这个帖子,是创建存储过程时发现的:
Python, “commands out of sync; you can’t run this command now”
这位也一样:
解决error:2014 Commands out of sync; you can’t run this command now
其他错误:
2014 - Commands out of sync; you can’t run this command now
解决问题:
综上所述,我感觉触发器、存储过程、SHOW等命令语句不能放一起执行。【待查】
解决方案是把语句分开执行。
在MySQL的官网手册,我发现了这个问题的说明:
B.5.2.14 Commands out of sync
If you get Commands out of sync; you can’t run this command now in
your client code, you are calling client functions in the wrong order.This can happen, for example, if you are using mysql_use_result() and
try to execute a new query before you have called mysql_free_result().
It can also happen if you try to execute two queries that return
data without calling mysql_use_result() or mysql_store_result() in
between.