Python,“命令不同步;您不能立即运行此命令" [英] Python, "commands out of sync; you can't run this command now"
问题描述
我有一个从Python(包装在Django中)执行的MySQL存储过程.当我尝试执行第二条语句时,出现错误命令不同步;您现在无法运行此命令".我目前无法提交交易.当我调用过程时,这只是一个问题.该怎么办?
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,“命令不同步;您不能立即运行此命令"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!