簡體   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