简体   繁体   中英

Getting return values from a MySQL stored procedure in Python, using MySQLdb

I've got a stored procedure in a MySQL database that simply updates a date column and returns the previous date. If I call this stored procedure from the MySQL client, it works fine, but when I try to call the stored procedure from Python using MySQLdb I can't seem to get it to give me the return value.

Here's the code to the stored procedure:

CREATE PROCEDURE test_stuff.get_lastpoll()
BEGIN
    DECLARE POLLTIME TIMESTAMP DEFAULT NULL;
    START TRANSACTION;

    SELECT poll_date_time
      FROM test_stuff.poll_table
      LIMIT 1 
      INTO POLLTIME
    FOR UPDATE;

    IF POLLTIME IS NULL THEN
        INSERT INTO 
               test_stuff.poll_table 
               (poll_date_time)
        VALUES 
               ( UTC_TIMESTAMP() );

        COMMIT;
        SELECT NULL as POLL_DATE_TIME;
    ELSE
        UPDATE test_stuff.poll_table SET poll_date_time = UTC_TIMESTAMP();
        COMMIT;
        SELECT DATE_FORMAT(POLLTIME, '%Y-%m-%d %H:%i:%s') as POLL_DATE_TIME;
    END IF;
END

The code I'm using to try to call the stored procedure is similar to this:

#!/usr/bin/python

import sys
import MySQLdb

try:
    mysql = MySQLdb.connect(user=User,passwd=Passwd,db="test_stuff")
    mysql_cursor = mysql.cursor()

    results=mysql_cursor.callproc( "get_lastpoll", () )
    print results

    mysql_cursor.close()
    mysql.close()

except MySQLdb.Error, e:
    print "MySQL Error %d:  %s" % ( e.args[0], e.args[1] )
    sys.exit(1)

I know that you can do IN and OUT parameters, but from what I can determine from the MySQLdb documentation, this isn't possible with MySQLdb. Does anyone have any clue how I could get the results of the stored procedure?

If I run it from a SQL tool, here's the output:

POLL_DATE_TIME       
-------------------  
2009-02-18 22:27:07  

If I run the Python script, it returns back an empty set, like this:

()

What I had to do is modify the Python code to use execute() instead of callproc(), and then use the fetchone() to get the results. I'm answering it myself since mluebke's answer wasn't entirely complete (even though it was helpful!).

mysql_cursor.execute( "call get_lastpoll();" )
results=mysql_cursor.fetchone()
print results[0]

This gives me the correct output:

2009-02-19 17:10:42

See https://stackoverflow.com/a/52715128/2391795 for advanced usage of fetchone , fetchall , etc.

callproc also works fine, you don't need to use execute :

mysql_cursor.callproc( "get_lastpoll", () )
result = mysql_cursor.fetchone()

You still have to fetch the results.

results = cursor.fetchone()

or

results = cursor.fetchall()

From the API documentation for the MySQLdb library. You will need to call cursor_obj.nextset() before you will see the result set returned by the stored procedure. This is because the call to the stored procedure creates a result set. The result sets returned by the stored procedure follow.

More info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM