python - 如何使用 MySQL-Connector 从 Python 中的 MySQL 存储过程中检索 out 参数?
问题描述
我无法使用 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
这是我的python函数(python 3.7),我只是不知道在光标对象上调用什么方法,或者如何处理这个。for 循环也不打印任何东西,我假设是因为游标存储的结果是空的。
任何帮助表示赞赏,谢谢。
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[3])
推荐阅读
- python - PyPI 谜题:-(连字符)和 . (点)分别用在什么情况下?
- reactjs - 超链接点击在反应组件React js中创建word文档
- python - 从不同的 df 列 pandas 创建具有匹配值的新列
- python - 在二叉树中查找未暴露的节点
- heroku - MERN Typescript 部署到 Heroku “没有运行 Web 进程”
- mysql - 大表上的慢sql更新查询
- c++ - C++ API 未处理异常:Microsoft C++ 异常:std::bad_alloc
- sockets - Coffeescript 等到从回调中设置标志,然后继续代码的下一部分
- segmentation-fault - 使用 taosdump 工具时 TDengine 分段错误
- javascript - 如何在客户端使用 ajax $.post 向 Mongodb 插入数据?