Questions MySQL Using Database Connectivity 2021
Questions MySQL Using Database Connectivity 2021
Questions MySQL Using Database Connectivity 2021
connectivity and display all the records from the table student2021
import mysql.connector
db = mysql.connector.connect(user=”root”, host=”localhost”,
password=''mysql”, database=”abps”')
cur = db.cursor()
cur.execute (”select *from student2021”)
data=cur.fetchall()
for i in data:
print(i)
Q2) Write a program to connect Python with MySQL using database connectivity and
display details of student who has scored marks greater than 75 from student2021
database table
import mysql.connector
db = mysql.connector.connect(user=”root”, host=”localhost”,
password=''mysql”, database=”abps”')
cur = db.cursor()
m=int(input(“Enter the marks”))
cur.execute (”select * from student2021 where marks>%s” % (m))
data=cur.fetchall()
for i in data:
print(i)
Q3) Write a program to connect Python with MySQL using database connectivity to
insert records & count records in student2021 database table
import mysql.connector
db = mysql.connector.connect(user=”root”, host=”localhost”,
password=''mysql”, database=”abps”')
cur = db.cursor()
r=int(input(“Enter roll number”))
n=int(input(“Enter Name”))
m=int(input(“Enter Marks”))
s=int(input(“Enter Stream”))
cur.execute (”insert into student2021 values ({},’{}’,{},’{}’)”.format(r,n,m,s))
db.commit()
print(“Row inserted successfully”)
Q4) Write a program to connect Python with MySQL using database connectivity to
update records of marks=99 for RollNo=10 from student2021 database table
import mysql.connector
db = mysql.connector.connect(user=”root”, host=”localhost”,
password=''mysql”, database=”abps”')
cur = db.cursor()
query=”update student2021 set marks=99 where RollNo=10”
cur.execute (”update student2021 set marks=99 where RollNo=10”)
db.commit()
print(“Row updated successfully”)
Q5) Write a program to connect Python with MySQL using database connectivity to
display and count the rows stored in student2021 database table
import mysql.connector
db = mysql.connector.connect(user=”root”, host=”localhost”,
password=''mysql”, database=”abps”)
cur = db.cursor()
cur.execute (”select *from student2021”)
data=cur.fetchall()
for i in data:
print(i)
print(“Total number of rows =”,cur.rowcount)
Important questions in Python Connectivity with MySQL