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

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()

Tags:

Python

Mysql