繁体   English   中英

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

[英]How do I retrieve an out parameter from a MySQL Stored Procedure in Python using MySQL-Connector?

我无法使用 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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM