Fetching records using fetchone() and fetchmany()
阿新 • • 發佈:2018-12-29
(Sponsors) 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!
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()
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 | from__future__importprint_functionimportMySQLdb asmytry:db=my.connect(host="127.0.0.1",user="root",passwd="",db="world")cursor=db.cursor()sql="select * from city where id < 10"number_of_rows=cursor.execute(sql)print(cursor.fetchone())# fetch the first row onlydb.close()exceptmy.DataError ase:print("DataError")print(e)exceptmy.InternalError ase:print("InternalError")print(e)exceptmy.IntegrityError ase:print("IntegrityError")print(e)exceptmy.OperationalError ase:print("OperationalError")print(e)exceptmy.NotSupportedError ase:print("NotSupportedError")print(e)exceptmy.ProgrammingError ase:print("ProgrammingError")print(e)except:print("Unknown error occurred") |
Looping over the result using fetchone()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 | from__future__importprint_functionimportMySQLdb asmytry:db=my.connect(host="127.0.0.1",user="root",passwd="",db="world")cursor=db.cursor()sql="select * from city where id < 10"number_of_rows=cursor.execute(sql)whileTrue:row=cursor.fetchone()ifrow==None:breakprint(row)db.close()exceptmy.DataError ase:print("DataError")print(e)exceptmy.InternalError ase:print("InternalError")print(e)exceptmy.IntegrityError ase:print("IntegrityError")print(e)exceptmy.OperationalError ase:print("OperationalError")print(e)exceptmy.NotSupportedError ase:print("NotSupportedError")print(e)exceptmy.ProgrammingError ase:print("ProgrammingError")print(e)except:print("Unknown error occurred") |
Using fetchmany()
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 | from__future__importprint_functionimportMySQLdb asmytry:db=my.connect(host="127.0.0.1",user="root",passwd="",db="world")cursor=db.cursor()sql="select * from city where id < 10"number_of_rows=cursor.execute(sql)print(cursor.fetchmany(2))# fetch first 2 rows onlydb.close()exceptmy.DataError ase:print("DataError")print(e)exceptmy.InternalError ase:print("InternalError")print(e)exceptmy.IntegrityError ase:print("IntegrityError")print(e)exceptmy.OperationalError ase:print("OperationalError")print(e)exceptmy.NotSupportedError ase:print("NotSupportedError")print(e)exceptmy.ProgrammingError ase:print("ProgrammingError")print(e)except:print("Unknown error occurred") |
Looping over the result using fetchmany()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 | from__future__importprint_functionimportMySQLdb asmytry:db=my.connect(host="127.0.0.1",user="root",passwd="",db="world")cursor=db.cursor()sql="select * from city where id < 10"number_of_rows=cursor.execute(sql)whileTrue:two_rows=cursor.fetchmany(2)iftwo_rows==():breakprint(two_rows)db.close()exceptmy.DataError ase:print("DataError")print(e)exceptmy.InternalError ase:print("InternalError")print(e)exceptmy.IntegrityError ase:print("IntegrityError")print(e)exceptmy.OperationalError ase:print("OperationalError")print(e)exceptmy.NotSupportedError ase:print("NotSupportedError")print(e)exceptmy.ProgrammingError ase:print("ProgrammingError")print(e)except:print("Unknown error occurred") |
Other Tutorials (Sponsors)
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!