MySQLdb Fetching results

Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

In the previous post we have seen how to execute sql queries using execute()  method. execute()  method returns affected rows, it doesn’t return the result. To fetch result we use the fetchall()  method of the cursor object

Syntax: cursor.fetchall()

On success it returns tuple of rows where each row is a tuple.

above code will print all the rows in the city table.

You can also use for loop to iterate over the results.

Some more examples.

Example 1:

Example 2:

 

Other Tutorials

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Leave a Reply

2 Comments on "MySQLdb Fetching results"

Notify of
avatar

Sort by:   newest | oldest | most voted
Ken Clement
Guest
Ken Clement
1 year 4 months ago

Syntax: cursor.fethcall() should instead read: Syntax: cursor.fetchall()

There is a letter transposition in fetchall – should be ‘ch’ instead of ‘hc’

wpDiscuz