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

Source: Internet
Author: User

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 wowould like to fetch the rows of the result.

Solution

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

'''Python cnx = mysql. connector. connect (user = 'Scott ', password = 'tiger', database = 'mining') cur = cnx. cursor () cur.exe cute ("drop procedure if exists slave_status") proc = "create procedure slave_status () begin show slave status; END" cur.exe cute () cur. call ("slave_status ")

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

The result from the above wocould be:

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

Discussion

Thestored_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 coshould also usewith_rowsCursor property to check if the cursor actually cocould return rows or not (for example, for SELECT statements). An example is provided in the documentation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.