☞ We will perform the fetch query on the table given below.
We will use the following in this section.
☞ Example :
import mysql.connector as m
mydb = m.connect(host='localhost', user='root', password='in$pireweb$oft', database='school')
cur = mydb.cursor()
a = float(input("Enter fees : "))
b = input("Enter gender[M/F] : ")
s="SELECT * FROM student WHERE fees <= '{}' or gender = '{}' ".format(a,b)
cur.execute(s)
OR
s="SELECT * FROM student WHERE fees <= %s or gender = %s "
rec=(a,b)
cur.execute(s,rec)
record = cur.fetchone() #returns one record at a time
no_rec = cur.rowcount #returns no. of record
print(record)
print("No. of records = ",no_rec)
☞ Output :
☞ Example :
import mysql.connector as m
mydb = m.connect(host='localhost', user='root', password='in$pireweb$oft', database='school')
cur = mydb.cursor()
a = float(input("Enter fees : "))
b = input("Enter gender[M/F] : ")
s="SELECT * FROM student WHERE fees <= '{}' or gender = '{}' ".format(a,b)
cur.execute(s)
OR
s="SELECT * FROM student WHERE fees <= %s or gender = %s "
rec=(a,b)
cur.execute(s,rec)
record = cur.fetchall() #list of records
no_rec = cur.rowcount
for i in record:
print(i)
print("No. of records = ",no_rec)
☞ Output :
☞ Example :
import mysql.connector as m
mydb = m.connect(host='localhost', user='root', password='in$pireweb$oft', database='school')
cur = mydb.cursor()
a = float(input("Enter fees : "))
b = input("Enter gender[M/F] : ")
s="SELECT * FROM student WHERE fees <= '{}' or gender = '{}' ".format(a,b)
cur.execute(s)
OR
s="SELECT * FROM student WHERE fees <= %s or gender = %s "
rec=(a,b)
cur.execute(s,rec)
record = cur.fetchmany(2) #list of records
no_rec = cur.rowcount
for i in record:
print(i)
print("No. of records = ",no_rec)
☞ Output :