Fetching records using fetchone() and fetchmany()

Up until now we have been using fetchall()  method of cursor object to fetch the records. This process of accessing all records in one go is not every efficient. As a result MySQLdb has fetchone() and fetchmany() methods of cursor object to fetch records more efficiently.

Method Description
fetchone() This method returns one record as a tuple, If there are no more records then it returns None
fetchmany(number_of_records) This method accepts number of records to fetch and returns tuple where each records itself is a tuple. If there are not more records then it returns an empty tuple.

Using fetchone()

Looping over the result using fetchone() 

Using fetchmany()

Looping over the result using fetchmany()

 

Leave a Reply

Be the First to Comment!

Notify of
avatar

wpDiscuz