程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> Snippet: Fetching results after calling stored procedures using MySQL Connector/Python,snippetfetching

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

編輯:MySQL綜合教程

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


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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved