[ACCEPTED]-Getting return values from a MySQL stored procedure in Python, using MySQLdb-mysql

Accepted answer
Score: 12

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

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

This gives me the correct 1 output:

2009-02-19 17:10:42

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

Score: 8

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

mysql_cursor.callproc( "get_lastpoll", () )
result = mysql_cursor.fetchone()
Score: 7

You still have to fetch the results.

results = cursor.fetchone()

or

results = cursor.fetchall()

0

Score: 0

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

More info

More Related questions