Snippet: Fetching results after calling stored procedures using MySQL Connector/Python

https://geert.vanderkelen.org/2014/results-after-procedure-call/

Problem

Using MySQL Connector/Python, you are calling a stored procedure which is also selecting data and you would like to fetch the rows of the result.

Solution

For this example we create a stored procedure which is executing SHOW SLAVE STATUS.

```python cnx = mysql.connector.connect(user=’scott’, password=’tiger’, database=’mining’) cur = cnx.cursor() cur.execute(“DROP PROCEDURE IF EXISTS slave_status”) proc = “CREATE PROCEDURE slave_status () BEGIN SHOW SLAVE STATUS; END” cur.execute() cur.call(“slave_status”)

for result_cursor in cur.stored_results(): for row in result_cursor: print(row[0]) ```

The result from the above would be:

shell $ python foo.py Waiting for master to send event

Discussion

The stored_results() method of cursor object is retiring an iterator object which will go over the results proceeded after calling the stored procedure. Each result is actually a MySQLCursorBuffered object.

You could also use the with_rows cursor property to check if the cursor actually could return rows or not (for example, for SELECT statements). An example is provided in the documentation.

时间: 2024-08-17 01:05:50

Snippet: Fetching results after calling stored procedures using MySQL Connector/Python的相关文章

An Introduction to Stored Procedures in MySQL 5

https://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843 MySQL 5 introduced a plethora of new features - stored procedures being one of the most significant. In this tutorial, we will focus on what they are, and h

Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python

f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can follow the MySQL stored procedures tutorial. We will create two stored procedures for the demonstration in this tutorial. The first stored procedure gets

MySQL Error Handling in Stored Procedures 2

Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in stored procedures. When an error occurs inside a stored procedure, it is important to handle it appropriately, such as continuing or exiting the c

关于Natively Compiled Stored Procedures的优化

Interpreted Transact-SQL stored procedures are compiled at first execution, in contrast to natively compiled stored procedures, which are compiled at create time. When interpreted stored procedures are compiled at invocation, the values of the parame

[MySQL] Stored Procedures

Stored routines (procedures and functions) can be particularly useful in certain situations: When multiple client applications are written in different languages or work on different platforms, but need to perform the same database operations. When s

mysql Error Handling and Raising in Stored Procedures

MySQL的存储过程错误捕获方式和Oracle的有很大的不同. MySQL中可以使用DECLARE关键字来定义处理程序.其基本语法如下: DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement handler_type: CONTINUE | EXIT condition_value: SQLSTATE [VALUE] sqlstate_value | condition_name | SQLWARNING | NO

The return types for the following stored procedures could not be detected

1.使用dbml映射数据库,添加存储过程到dbml文件时报错. 2.原因:存储过程中使用了临时表 3.解决方案 3.1 通过自定义表值变量实现 Ex: DECLARE @TempTable TABLE ( AttributeID INT, Value NVARCHAR(200) ) INSERT INTO @TempTable Select * from Attribute OR --Execute SP and insert results into @TempTable INSERT INT

Fluent Nhibernate and Stored Procedures

sql:存储过程 DROP TABLE Department GO CREATE TABLE Department ( Id INT IDENTITY(1,1) PRIMARY KEY, DepName VARCHAR(50), PhoneNumber VARCHAR(50) ) GO CREATE PROCEDURE [dbo].[GetDepartmentId] ( @Id INT ) AS BEGIN SELECT * FROM Department WHERE Department.Id

SQL Server :Stored procedures存储过程初级篇

对于SQL Server,我是个拿来主义.很多底层的原理并不了解,就直接模仿拿着来用了,到了报错的时候,才去找原因进而逐步深入底层.我想,是每一次的报错,逼着我一点点进步的吧. 近期由于项目的原因,我需要写一些存储过程.同时学校还开了一门<数据库系统>的课程.两者结合满足了我浓厚的兴趣. 下面写写我对存储过程的简单认识. 一.基础知识: 1.select,insert,update,delete 的语法 ,这是核心中的核心 2.一点点的编程基础:了解基本的控制流程语言,如if...else,w