You are on page 1of 7

PYTHON PROGRAMS WITH MYSQL CONNECTIVITY

EX:NO:1a)
Aim:
Write a Python program to create a table in MYSQL using database connectivity
Steps to ee!"te p#t$o% p&o'&ams:
1. Open the Gedit editor and rite the program
!. Save the program ith .py as e"tension
#. $"ecute the program using python %ilename.py
P&o'&am
&'(usr(bin(python
import MySQLdb
& Open database connection
db ) MySQLdb.connect*+localhost+,+root+,++,+student+ -
& prepare a cursor ob.ect using cursor*- method
cursor ) db.cursor*-
& /rop table i% it already e"ist using e"ecute*- method.
cursor.e"ecute*+/0OP 123L$ 45 $64S1S college +-
& 7reate table as per re8uirement
s8l ) +++70$21$ 123L$ college * 540S19:2M$ 7;20*!<- :O1 :=LL,L2S19:2M$
7;20*!<-, 2G$ 4:1, S$6 7;20*1--+++
cursor.e"ecute*s8l-
& disconnect %rom server
db.close*-
O"tp"t:
Go to terminal>
?%osslab@%osslab ABC python /bpython.py
?%osslab@%osslab ABC su D
Passord> EEEEEE
Service mys8ld start
Welcome to the MySQL monitor. 7ommands end ith F or Gg.
Your MySQL connection id is !H
Server version> I.1.I1 Source distribution
7opyright *c- !<<<, !<1<, Oracle and(or its a%%iliates. 2ll rights reserved.
1his so%tare comes ith 23SOL=1$LY :O W2002:1Y. 1his is %ree so%tare,
and you are elcome to modi%y and redistribute it under the GPL v! license
1ype JhelpFJ or JGhJ %or help. 1ype JGcJ to clear the current input statement.
mys8lK sho databasesF
/atabase
L
MDDDDDDDDDDDDDDDDDDDDM
L in%ormation9schema L
L 1$S1/3
L
L college
L
L employee
L
L mys8l L
L sample L
L steel
L
L student
L test
L
L
MDDDDDDDDDDDDDDDDDDDDM
N ros in set *<.<< sec-
mys8lK use studentF
mys8lK sho tablesF
MDDDDDDDDDDDDDDDDDDDM
L 1ables9in9student L
MDDDDDDDDDDDDDDDDDDDM
L $MPLOY$$
L
L courses L
L student L
MDDDDDDDDDDDDDDDDDDDM
# ros in set *<.<< sec-
mys8lKK desc collegeF
MDDDDDDDDDDDDMDDDDDDDDDDMDDDDDDMDDDDDMDDDDDDDDDMDDDDDDDM
L 5ield L 1ype L :ull L Oey L /e%ault L $"tra L
MDDDDDDDDDDDDMDDDDDDDDDDMDDDDDDMDDDDDMDDDDDDDDDMDDDDDDDM
L 540S19:2M$ L char*!<- L :O L L :=LL L L
L L2S19:2M$ L char*!<- L Y$S L L :=LL L L
L 2G$ L int*11- L Y$S L L :=LL L L
L S$6 L char*I- L Y$S L L :=LL L L
MDDDDDDDDDDDDMDDDDDDDDDDMDDDDDDMDDDDDMDDDDDDDDDMDDDDDDDM
P ros in set *<.<I sec-
Res"(t
1hus the python program as e"ecuted and table as created in MYSQL.
EX:NO:1))
Aim:
Write a Python program to create a table in MYSQL using database connectivity
Steps to ee!"te p#t$o% p&o'&ams:
1. Open the Gedit editor and rite the program
!. Save the program ith .py as e"tension
#. $"ecute the program using python %ilename.py
P&o'&am
&'(usr(bin(python
import MySQLdb
& Open database connection
db ) MySQLdb.connect*+localhost+,+root+,++,+student+ -
& prepare a cursor ob.ect using cursor*- methods
cursor ) db.cursor*-
& Prepare SQL 8uery to 4:S$01 a record into the database.
s8l ) +++4:S$01 4:1O college*540S19:2M$,L2S19:2M$, 2G$, S$6-
Q2L=$S *JSachinJ, J1endulRarJ, #I, JMJ-+++
try>
& $"ecute the SQL command
cursor.e"ecute*s8l-
& 7ommit your changes in the database
db.commit*-
e"cept>
& 0ollbacR in case there is any error
db.rollbacR*-
& disconnect %rom server
db.close*-
O"tp"t:
Go to terminal>
?%osslab@%osslab ABC python /binser.py
?%osslab@%osslab ABC su D
Passord> EEEEEE
Service mys8ld start
Welcome to the MySQL monitor. 7ommands end ith F or Gg.
Your MySQL connection id is !H
Server version> I.1.I1 Source distribution
7opyright *c- !<<<, !<1<, Oracle and(or its a%%iliates. 2ll rights reserved.
1his so%tare comes ith 23SOL=1$LY :O W2002:1Y. 1his is %ree so%tare,
and you are elcome to modi%y and redistribute it under the GPL v! license
1ype JhelpFJ or JGhJ %or help. 1ype JGcJ to clear the current input statement.
mys8lK sho databasesF
/atabase
L
MDDDDDDDDDDDDDDDDDDDDM
L in%ormation9schema L
L 1$S1/3
L
L college
L
L employee
L
L mys8l L
L sample L
L steel
L
L student
L test
L
L
MDDDDDDDDDDDDDDDDDDDDM
N ros in set *<.<< sec-
mys8lK use studentF
mys8lK select E %rom collegeF
MDDDDDDDDDDDDMDDDDDDDDDDDMDDDDDDMDDDDDDMDDDDDDDDM
L 540S19:2M$ L L2S19:2M$ L 2G$ L S$6 L 4:7OM$ L
MDDDDDDDDDDDDMDDDDDDDDDDDMDDDDDDMDDDDDDMDDDDDDDDM
L Sachin L 1endulRar L #I L M L
0esult
1hus the python program as e"ecuted and record as inserted into MYSQL database.
EX:NO:1!)
Aim:
Write a Python program to retrive data %rom a table using database connectivity
Steps to ee!"te p#t$o% p&o'&ams:
1. Open the Gedit editor and rite the program
!. Save the program ith .py as e"tension
#. $"ecute the program using python %ilename.py
P&o'&am
&'(usr(bin(python
& import the MySQLdb and sys modules
import MySQLdb
import sys
& open a database connection
db ) MySQLdb.connect*+localhost+,+root+,++,+student+ -
& prepare a cursor ob.ect using cursor*- method
cursor ) db.cursor *-
& e"ecute the SQL 8uery using e"ecute*- method.
cursor.e"ecute *+select 540S19:2M$,540S19:2M$,2G$,S$6,4:7OM$ %rom student+-
& %etch all o% the ros %rom the 8uery
data ) cursor.%etchall *-
& print the ros
%or ro in data >
print ro?<B, ro?1B,ro?!B,ro?#B,ro?PB
& close the cursor ob.ect
cursor.close *-
& close the connection
db.close *-
& e"it the program
sys.e"it*-
O"tp"t:
Go to terminal>
?%osslab@%osslab ABC python /bser.py
Sachin 1endulRar #< M !<<<<
/honi MS !I M !I<<<
2.ith Oumar P< M #<<<<
0esult
1hus the python program as e"ecuted and record as retrived using MYSQL database

You might also like