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