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!


2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
adminKen Clement Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Ken Clement
Guest
Ken Clement

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

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