Uploaded by Hema R

PYTHON PROGRAM CLSS XII COMPTER SCIENCE -MYSQL CONNECTIVITY

advertisement
Write a program to generate 3 random integers between 100 and 999 which is divisible by 5.
Ans.
import random
a1=random.randint(100,399)
a2=random.randint(100,399)
a3=random.randint(100,399)
print('Three random numbers are',a1,a2,a3)
PYTHON –MYSQL CONNECTIVITY STUB PROGRAM
RECORD PROGRAM (LAST 5 PROGRAMS)
1. Write the steps to perform an Insert query in database connectivity application.Table
‘student’ values are rollno, name, age (10,’Raman’,26).
RollNo – integer
Name – string
Age – integer
Note the following to establish connectivity between Python and MYSQL:
 Username is root
 Password is 1234
 The table exists in a MYSQL database, named school.
 The details (RollNo, Name, Class, and Age) are to be accepted from the user.
Write the following missing statements to complete the code:
Statement 1 – To give an alias name for connector package
Statement 2 – to form the cursor object
Statement 3 – to execute the command that inserts the record in the table Student
Statement 4- to add the record permanently in the database
import mysql.connector as __________#Statement 1
conn= mydb.connect(host=”localhost”, user=”root”, passwd=”1234”)
cur= ______#Statement 2
cur.execute
#Statement 3
#Statement 4
print("Data Added successfully")
Answer:
import mysql.connector as mydb
conn= mydb.connect(host=”localhost”, user=”root”, passwd=”1234”)
cur=conn.cursor()
cur.execute(“INSERT INTO student values(10,’Ashok’,26);”)
cur.commit()
print("Data Added successfully")
a) 2. Write the steps to perform an Insert query in database connectivity application.
Table ‘student’ values are rollno, name, age (10,’Raman’,26)
RollNo – integer
Name – string
Age – integer
Note the following to establish connectivity between Python and MYSQL:
 Username is root
 Password is 1234
 The table exists in a MYSQL database named school.
 The details (RollNo, Name, Clas and Age) are to be accepted from the user.
Write the following missing statements to fetch all records of a table Student at run time:
Statement 1 – to form the cursor object
Statement 2 – to execute the command that fetch all records of a table Student .
Statement 3- to fetch all records
Statement4-To close connection between Python and MYSQL
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="123"
,database="school")
mycursor=
# Statement 1
mycursor.
# Statement 2
myrecords=_
# Statement 3
for x in myrecords:
print (x)
mydb.___________ # Statement 4
Answers:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="123"
,database="school")
mycursor=mydb.cursor()
mycursor.execute("select * from student")
myrecords=mycursor.fetchall()
for x in myrecords:
print (x)
mydb.close()
3. ) Consider the following Python code is written to access the details of
employee, whose employee number is passed to function:
Complete the missing statements:
import mysql.connector
def Search(eno):
mydb=mysql.connector.-------(host="localhost", user="root", passwd=
"system", database="DB")
#Statement 1
if mydb._____________():
#Statement 2
print(‘Connection established successfully’)
else :
print(‘Connection not established’)
return
mycursor=mydb.cursor()
query="select * from emp where empno=__".format(eno) #Statement 3
mycursor.execute(query)
results = mycursor. ________________ #Statement 4
print(results)
answer:
#Statement 1-Connect
#Statement 2-is_connected ()
#Statement 3-
.{ }
#Statement 4-fetchone()
4) The code given below inserts the following record in the table Employee:
ENo – integer
EName – string
Dept – string
Salary – integer
Note the following to establish connectivity between Python and MYSQL:
* Username is root
* Password is tiger
* The table exists in a MYSQL database named Company
* The details (ENo, EName, Dept, Salary) are to be accepted from the user.
Write the following missing statements to complete the code:
Statement 1-To establish connectivity between Python and MYSQL
Statement 2 – to form the cursor object
Statement 3 – to execute the command that inserts the record in the table Employee.
Statement 4- to add the record permanently in the database
import mysql.connector as mysql
def sql_data():
con1=mysql.connect(____________) #Statement 1
mycursor = ----------------- #Statement 2
eno=int(input("Enter Employee No :: "))
ename=input("Enter Name :: ")
dept=input("Enter Dept :: ")
salary=int(input("Enter Salary :: "))
query="insert into employee values({},'{}','{}',{})".format(eno,ename,dept,salary)
----------------------#Statement 3
-----------------------# Statement 4
print("Data Added successfully")
Answers :
1)( host="localhost”, user="root", password="tiger”, database="Company")
2)con1.cursor()
2) mycursor.execute(query)
3) con1.commit()
5) The code given below reads the following record from the table named Employee and displays only
those records who have salary greater than 15,000:
ENo – integer
EName – string
Dept – string
Salary – integer
Note the following to establish connectivity between Python and MYSQL:
* Username is root
* Password is tiger
* The table exists in a MYSQL database named Company.
Write the following missing statements to complete the code:
Statement 1 – to form the cursor object
Statement 2 – to execute the query that extracts records of those employees
whose salary is greater than 15,000.
Statement 3- to read the complete result of the query (records whose salary
is greater than 15,000) into the object named data, from the table employee
in the database.
Statement 4-To get the number of rows retrieved from cursor/resultset
import mysql.connector as mysql
def sql_data():
con1=mysql.connect(host="localhost",user="root",password="tiger
", database="Company")
mycursor = __________ #Statement 1
print("Employees whose salary greater than 15000 are : ")
___________________ #Statement 2
data = __________________ #Statement 3
for i in data:
print(i)
print(“Row count is”,___________)#Statement 4
con1.close()
Answers :
1)con1.cursor()
2) mycursor.execute(“ select * from employee where salary>1500;”)
3) con1.fetchall()
4)mycursor.rowcount
Download