20200907-XII-Python With MySQL-1 of 2-Handout
20200907-XII-Python With MySQL-1 of 2-Handout
Python Code – 1
#Python code to retrieve all the rows from the relation RESULT
#Import the package mysql.connector
import mysql.connector as SqlCon
OUTPUT
MySQL database is successfully connected
Number of records retrieved from the table : 10
Then all the rows will be displayed
Python Code – 2
#Alternate Python code to retrieve one record at a time
#from the relation RESULT
#Import the package mysql.connector
import mysql.connector as SqlCon
MYCon = SqlCon.connect(host=”localhost”, user=”root”, passwd=”gms”,
database = “AECS”)
if MyCon.is_connected() :
print(“MySQL database is successfully connected”)
else:
print(“Error in connecting to MySQL database”)
OUTPUT
MySQL database is successfully connected
First, all the rows will be displayed
Then,
Number of records retrieved from the table : 10 will be displayed
Python Code – 3
#Python code to retrieve all the record and display few columns
#from the relation RESULT
#Import the package mysql.connector
import mysql.connector
conn = mysql.connector.connect(host=”localhost”, user=”root”, passwd=”gms”,
database = “AECS”)
if conn.is_connected() :
print(“MySQL database is successfully connected”)
else:
print(“Error in connecting to MySQL database”)
OUTPUT
MySQL database is successfully connected
First,
Number of records retrieved from the table : 10 will be displayed
Then,
RollNo, Name, Subject and Marks of each student will be displayed
if conn.is_connected() :
print(“MySQL database is successfully connected”)
else:
print(“Error in connecting to MySQL database”)
#Create cursor instance and execute SQL query
cob = conn.cursor()
sq = “delete from result where rno = ‘%d’”
arg = (rno)
try:
cob.execute(sq%arg)
cob.commit() #save the changes in the database
print(“1 row deleted…”)
except:
cob.rollback() #un-save the changes in the database
finally:
cob.close() #close the connection
conn.close()
#__main__
rn = int(input(‘Enter roll number =’))
Delete_Rows(rn) #call the function
OUTPUT
MySQL database is successfully connected
Enter roll number = 1012
1 row deleted…
Python Code – 5
#Python code to update a record / row from the relation RESULT
import mysql.connector as sqcon
try:
cob.execute(sq%arg)
cob.commit() #save the changes in the database
print(“Marks in Physics updated…”)
except:
cob.rollback() #un-save the changes in the database
finally:
cob.close() #close the connection
conn.close()
#__main__
st = input(‘Enter Subject : ’)
Update_Rows(st) #call the function
OUTPUT
MySQL database is successfully connected
Enter Subject : Physics
Marks in Physics updated…
Python Code – 6
#Python code to create a new table result in MySQL database
import mysql.connector as sqcon
#Function to create a new table result
def Create_Table():
conn = sqcon.connector.connect(host=”localhost”, user=”root”,
passwd=”gms”, database = “AECS”)
if conn.is_connected() :
print(“MySQL database is successfully connected”)
else:
print(“Error in connecting to MySQL database”)
#Create cursor instance and execute SQL query
cob = conn.cursor()
cob.execute(drop table if exists result)
sq = “create table result(rollno int(4), name varchar2(20), class
char(5), subject varchar(15), doe date, marks int(3))”
cob.execute(sq)
print(‘Table created …’)
cob.close() #close the connection
conn.close()
#__MAIN__
Create_Table()
OUTPUT
MySQL database is successfully connected
Table created...
finally:
cob.close() #close the connection
conn.close()
#__MAIN__
n = int(input(“How many rows to be inserted ? ”))
for i in range(n):
rn = int(input(‘Enter rollno : ‘))
nam = input(‘Enter name : ‘)
cl = input(‘Enter class : ‘)
sub = input(‘Enter subject : ‘)
dt = input(‘Enter doe : ‘)
mks = int(input(‘Enter marks : ‘))
Insert_Rows(rn, nam, cl, sub, dt, mks)
print(“_________________________________”)
OUTPUT
MySQL database is successfully connected
How many rows to be inserted ? 1
Enter rollno : 1025
Enter name : Dinesh
Enter class : XII-C
Enter subject : Accountancy
Enter doe : 14-03-2020
Enter marks : 99
_________________________________
1 row inserted …
0o0o0o0o0o0o0o0