出于某种奇怪的原因,我无法从 Python 测试应用程序中的 callproc 调用中获取结果。 MqSQL 5.2.47 中的存储过程如下所示:
CREATE PROCEDURE `mytestdb`.`getperson` (IN personid INT)
BEGIN
select person.person_id,
person.person_fname,
person.person_mi,
person.person_lname,
person.persongender_id,
person.personjob_id
from person
where person.person_id = personid;
END
现在,使用 PyCharm 和 Python 3.3,在调用此存储过程时我似乎无法检索任何内容。这段代码得到了我想要的结果:
import mysql.connector
cnx = mysql.connector.connect(user='root', host='127.0.0.1', database='mytestdb')
cnx._open_connection()
cursor = cnx.cursor()
cursor.execute("select * from person where person.person_id = 1")
people = cursor.fetchall()
for person in people:
print(person)
cnx.close()
但是这段代码带有cursor.fetchall()或cursor.fetchone()...
import mysql.connector
cnx = mysql.connector.connect(user='root', host='127.0.0.1', database='mytestdb')
cnx._open_connection()
cursor = cnx.cursor()
cursor.callproc("getperson", [1])
people = cursor.fetchall()
for person in people:
print(person)
cnx.close()
...返回“mysql.connector.errors.InterfaceError:没有可从中获取的结果集。”使用cursor.execute()方法还有一个额外的奇怪行为,就像这样......
import mysql.connector
cnx = mysql.connector.connect(user='root', host='127.0.0.1', database='mytestdb')
cnx._open_connection()
cursor = cnx.cursor()
cursor.execute("call getperson(1)")
people = cursor.fetchall()
for person in people:
print(person)
cnx.close()
...因为它产生“mysql.connector.errors.InterfaceError:对具有多个查询的语句使用cmd_query_iter”,然后是“mysql.connector.errors.InterfaceError:执行多个语句时使用multi=True”,尽管事实上我只返回一个查询结果而不是多个结果集。 MySQL Python 连接器是否将对存储过程的执行调用视为双重查询?我如何调用存储过程并获取结果?我真的不想在我的代码中使用动态 SQL。提前感谢您的任何建议!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您是否尝试过选择其中一个结果集?
for result in cursor.stored_results(): people = result.fetchall()即使您只有一个
SELECTstmt,它也可能会分配多个结果集。我知道在 PHP 的 MySQLi 存储过程中这样做是为了允许 INOUT 和 OUT 变量返回(同样,你没有,但也许它无论如何都在分配)。我正在使用的完整代码(正在运行)是:
import mysql.connector cnx = mysql.connector.connect(user='me',password='pw',host='localhost',database='mydb') cnx._open_connection() cursor = cnx.cursor() cursor.callproc("getperson",[1]) for result in cursor.stored_results(): people=result.fetchall() for person in people: print person cnx.close()