如何使用 MySQL-Connector 从 Python 中的 MySQL 存储过程中检索输出参数?

问题描述:

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

I'm having trouble accessing an out parameters values from a stored procedure in MySQL(8x) using Python(3.7) and sql connector (8x).

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

My stored procedure is an update procedure and it is working, however I don't know how to get the out value in code.

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

Here is my stored procedure...I simply want to access the out parameter named success in python.

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 循环也不打印任何内容,我假设是因为游标存储的结果为空.

Here is my python function (python 3.7), I just don't know what method to call on the cursor object, or how to approach this. The for loop also doesn't print anything, I'm assuming because the cursor stored results are empty.

感谢任何帮助,谢谢.

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()

这会给你第四个参数

查看官方文档一个>

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

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

    print(result_args[4])