如何使用 MySQL-Connector 从 Python 中的 MySQL 存储过程中检索输出参数? [英] How do I retrieve an out parameter from a MySQL Stored Procedure in Python using MySQL-Connector?

查看:52
本文介绍了如何使用 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])

这篇关于如何使用 MySQL-Connector 从 Python 中的 MySQL 存储过程中检索输出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆