首页 > 解决方案 > 如何使用 MySQL-Connector 从 Python 中的 MySQL 存储过程中检索 out 参数?

问题描述

我无法使用 Python(3.7) 和 sql 连接器 (8x) 从 MySQL(8x) 中的存储过程访问输出参数值。

我的存储过程是一个更新过程,它正在工作,但是我不知道如何在代码中获取输出值。

这是我的存储过程...我只是想在 python中访问名为success的 out 参数。

CREATE DEFINER=`root`@`localhost` PROCEDURE `update_due_date`(param_bookId int, param_cardNumber int, param_dueDate date, out success int)
BEGIN
    UPDATE tbl_book_loans
    SET dueDate = param_dueDate
    WHERE bookId = param_bookId and cardNo = param_cardNumber;        
    SET success = 777666;    
END

这是我的python函数(python 3.7),我只是不知道在光标对象上调用什么方法,或者如何处理这个。for 循环也不打印任何东西,我假设是因为游标存储的结果是空的。

任何帮助表示赞赏,谢谢。

def updateDueDate(bookId, cardNo, newDueDate):
    args = [bookId, cardNo, newDueDate, 0]

    myCursor.callproc(
        'update_due_date', args)
    myCursor.execute()

    for result in myCursor.stored_results():
        print(result.fetchall())
    cnx.commit()

标签: pythonmysqlmysql-connector

解决方案


这将为您提供第四个参数

见官方文档

def updateDueDate(bookId, cardNo, newDueDate):
    args = [bookId, cardNo, newDueDate, 0]

    result_args = cursor.callproc('update_due_date', args)

    print(result_args[3])

推荐阅读