Check sibling questions
Computer Science - Class 12
Chapter 15 - Interface of Python with SQL Database

This Question asked in CBSE SQP 2021

Differentiate between fetchone() and fetchall () methods with suitable examples for each.

 

Answer:

Fetchone( )

Fetchall( )

Fetches one row from the resultset. It will return None if there are no more rows in the resultset.

Fetches all the rows from the resultset. An empty list is returned if there is no record in the resultset.

Example:

import mysql.connector 

mydb=mysql.connector.connect("localhost","root","root","ABC") cursor=mydb.cursor()

sql="select * from employee"

cursor.execute(sql)

result=cursor.fetchone()

print(result)

cursor.close()

mydb.close()

Example:

import mysql.connector 

mydb=mysql.connector.connect("localhost","root","root","ABC")

cursor=mydb.cursor()

sql="select * from employee"

cursor.execute(sql)

result=cursor.fetchall()

for row in result:

    print(row)

cursor.close()

mydb.close()



Learn Intergation from Davneet Sir - Live lectures starting soon!

Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 12 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.