问题描述
我有一个从 Python 执行的 MySQL 存储过程(封装在 Django 中).当我尝试执行第二条语句时,出现错误命令不同步;您现在无法运行此命令".我此时无法提交事务.这只是我调用程序时的一个问题.怎么办?
I have a MySQL stored procedure that is executed from Python (wrapped in Django). I get the error "commands out of sync; you can't run this command now" when I try to execute the second statement. I cannot commit the transaction at this point. This is only an issue when I call a procedure. What to do?
cursor.callproc('my_mysql_procedure', [some_id,])
result = cursor.fetchall()
for r in result:
do something
cursor.execute("select * from some_table")
result = cursor.fetchall()
我被要求发布 MySQL 程序.我已经让它变得超级简单,但我仍然看到同样的问题
I've been asked to post the MySQL procedure. I have made it super-simple and I still see the same problem
delimiter $$
create procedure my_mysql_procedure(p_page_id int)
begin
select 1
from dual;
end$$
delimiter ;
推荐答案
感谢 JoshuaBoshi 的回答,解决了问题.调用该过程后,我不得不关闭游标并再次打开它,然后才能使用它来执行另一条语句:
Thanks to JoshuaBoshi for his answer, which solved the problem. After calling the procedure, I had to close the cursor and open it again before using it to execute another statement:
cursor.close()
cursor = connection.cursor()
游标可以在fetchall()
之后立即关闭.结果集依然存在,可以循环遍历.
The cursor can be closed immediately after fetchall()
. The result set still remains and can be looped through.
这篇关于Python Mysql,“命令不同步;你现在不能运行这个命令"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!