Calling Stored Procedures using Connector/Python

What is Stored Procedure?

A Stored procedure is simply a set of SQL statements grouped together and given a name. Stored procedures are used in situations where you want to execute the same set of statements multiple number of times. We create stored procedures using the CREATE PROCEDURE statement. Stored procedures can also accept arguments. Once the stored procedure is created, we can invoke it using the CALL statement followed by the procedure name, followed by arguments (if any) inside the parentheses.

Here is a simple stored procedure that accepts a continent and returns the name of the countries in it.

Note that in the preceding snippet the con argument is preceded with the IN keyword, thus it is an input parameter. An input parameter is used to pass values to the stored procedure from the calling program. A stored procedure can change the value of the input parameter but this change will be not visible to calling the program when the procedure returns. If you want to return values from the stored procedures then declare the parameter as output parameter using the OUT keyword instead of IN. The initial value of the output parameter is NULL.

In addition to input and output parameters, there exists another kind parameter called input/output parameter. As the name suggests, an input/output parameter can be used as an input parameter as well as an output parameter. In other words, input/output parameter can be used to pass values to the stored procedures. Further, a stored procedure can modify the input/output parameter and return it to the calling program. To create input/output parameter precede the variable name with the INOUT keyword.

Let’s create a stored procedure that uses both input and output parameters.

Here is how this stored procedure works:

  1. The stored procedure accepts two arguments: country and info. The country is the input parameter and info is the output parameter.
  2. Next, we use the SELECT statement to fetch some relevant data about the country. To store the data into the output parameter we use the INTO clause.
  3. Finally, we print the top 10 populated cities of the country in descending order.

We now have two stored procedures in the world database. To view them type the following command:

Note: Some columns are in preceding output is omitted to save the space.

To call the get_country_by_continent stored procedure use the CALL statement as follows:

Similarly, we can call get_country_info as follows:

Notice how the output parameter (i.e @info) is passed to the procedure. After the procedure executes, we can print the value of the output parameter using the SELECT statement as follows:

Calling Stored Procedures with callproc()

To call stored procedures we use the callproc() method of the cursor object. The syntax of the callproc() method is as follows:

Argument Description
proc_name The stored procedure you want to call
args A sequence of arguments to pass to the stored procedure

On success, the callproc() returns a modified version of the input sequence. The input parameters (i.e. parameter coded with the IN keyword ) are left untouched but the input and output parameters (i.e. parameters coded with the OUT and INOUT keyword, respectively) may contain new values.

Once the stored procedure is executed, you can fetch the results using the stored_results() method of the cursor object. The stored_results() returns an iterator which can be used to iterate over the rows in the result set.

It is important to note that the result set produced by the stored procedure is fetched immediately and stored as MySQLCursorBuffered instances.

The following listing demonstrates how to use the callproc() in action:

Expected Output:

Notice that in the second call to callproc() method, we are passing None as the second argument this is because the get_country_info stored produced accepts two arguments (one input and one output).

Leave a Comment