MySQL Learning---Executing stored procedures using Python

Source: Internet
Author: User

Execute a stored procedure using Python

Execute a stored procedure using Python [Part 2]:

1. Execute the stored procedure to get the result set of the stored procedure
2. Set the return value to the @_ stored procedure name _ Number =

#!/usr/bin/env python#-*-coding:utf-8-*-import pymysqlconn = pymysql.connect (host= ' 127.0.0.1 ', port=3306, user= ' root ', passwd= ', db= ' Test_python ', charset= ' UTF8 ') cursor = Conn.cursor (cursor=pymysql.cursors.dictcursor) # Execute stored Procedure # @_p3_ 0 = 1, the equivalent of Python to help us create a variable, so the return value is placed in this variable, we need to manually get # @_p3_1 = @_p3_2 = 3# @_p3_3 = 0ret1 = Cursor.callproc (' P3 ', args= (1 , 2, 3, 0) # print (' Ret1: ', ret1) # Pass parameters past Ret1: (1, 2, 3, 0), while executing the stored procedure put the result in memory result = Cursor.fetchall () # Gets the return value from memory, [{' @_p3_0 ': 1, ' @_p3_3 ': 7, ' @_p3_1 ': 2, ' @_p3_2 ': 103}]print (' return parameter: ', result, ' \ n ') # Gets the parameters of the stored execution, because Python helps us put the result in the variable, So you need to get Ret2 = Cursor.execute ("select @_p3_0,@_p3_1,@_p3_2,@_p3_3") # print (' Ret2: ', Ret2) from inside the variable: # Nothing to do, Ret2:1result 2 = Cursor.fetchall () # Gets the return value, [{' @_p3_0 ': 1, ' @_p3_3 ': 7, ' @_p3_1 ': 2, ' @_p3_2 ': 103}]print (' returned result set: ', RESULT2) conn.c Ommit () # Because there are multiple SQL statements inside the stored procedure, there may be selete,insert and so on, so in order to ensure that the code is complete Cursor.close () conn.close ()

MySQL Learning---Executing stored procedures using Python

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.