60% found this document useful (5 votes)
4K views

Questions MySQL Using Database Connectivity 2021

The document contains 5 questions about writing Python programs to connect to a MySQL database and perform various operations: 1) A program to connect and display all records from the student2021 table 2) A program to connect and display records from student2021 with marks > 75 3) A program to connect, insert a record, and count records in the student2021 table 4) A program to connect and update the marks field to 99 where RollNo is 10 5) A program to connect, display all records, and count the number of rows in student2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
60% found this document useful (5 votes)
4K views

Questions MySQL Using Database Connectivity 2021

The document contains 5 questions about writing Python programs to connect to a MySQL database and perform various operations: 1) A program to connect and display all records from the student2021 table 2) A program to connect and display records from student2021 with marks > 75 3) A program to connect, insert a record, and count records in the student2021 table 4) A program to connect and update the marks field to 99 where RollNo is 10 5) A program to connect, display all records, and count the number of rows in student2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1) Write a program to connect Python with MySQL using database

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

1) Which package do we import in python to establish MySQL python


connectivity?
Ans: mysql.connector

2) What is the significant of using connect() function?


Ans: Establish a connection with MySQL Database

3) What is the role of execute() function?


Ans: The role is to execute the queries which are MySQL queries with Python
interface

4) What is the command to install mysql connector?


Ans: pip install mysql-connector

5) What is database connectivity?


Ans: It refers to the connection and communication between and application
and database system

6) What is database cursor? [cur=db.cursor()]


Ans: A database cursor is a special control structure that facilities the row by
row processing of the records from a table

7) Write a programe code to connect to a database?


Ans: import mysql.connector
db = mysql.connector.connect(user=”root”, host=”localhost”,
password=''mysql”)

(Note user, host :- values are default values)


password is mysql
8) What is fetchall() function refers?
Ans: To retrieve records from the database table sequences of sequences one
by one records

You might also like