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

自闭症网瘾萝莉.ら 提交于 2021-02-08 09:49:42

问题


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.

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

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.

Any help is appreciated, thanks.

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

回答1:


This will get you the fourth parameter

See official documentation

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

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

    print(result_args[4])


来源:https://stackoverflow.com/questions/61977627/how-do-i-retrieve-an-out-parameter-from-a-mysql-stored-procedure-in-python-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!