0% found this document useful (0 votes)
108 views

Creating Databases Using Python and SQL Module PDF

This document shows Python code for creating a database and table using the MySQL connector module. It defines functions to create a database called Restaurant, create a table within it to store stationary item data, insert, display, update, delete and search records in the table. A menu function allows the user to choose these options and call the respective functions.

Uploaded by

Taruun R Malik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Creating Databases Using Python and SQL Module PDF

This document shows Python code for creating a database and table using the MySQL connector module. It defines functions to create a database called Restaurant, create a table within it to store stationary item data, insert, display, update, delete and search records in the table. A menu function allows the user to choose these options and call the respective functions.

Uploaded by

Taruun R Malik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CREATING DATABASES USING PYTHON AND SQL MODULE

CODE:

def createDatabase():
import mysql.connector
con=mysql.connector.connect(host="localhost",
user="root",
password="mysql25"
)
if con.is_connected():
print("connection established ......")
else:
print("Error in connection .......")
cur=con.cursor()
cur.execute("create database Restaurant")
cur.execute("show databases")
d=cur.fetchall()
print(d)
print("_______________________________")
for i in d:
print(i)

def createTable():
import mysql.connector
con=mysql.connector.connect(host="localhost",
user="root",
password="mysql25",
database='Restaurant'
)
cur=con.cursor()
cur.execute("create table stationary(i_no int primary key,i_name
varchar(10),price float)")
'''
cur.execute("desc stationary")
d2=cur.fetchall()
print(d2)'''

def Insert():
import mysql.connector
con=mysql.connector.connect(host="localhost",
user="root",
password="mysql25",
database='Restaurant'
)
cur=con.cursor()
while True:
no=int(input("Enter Item no. : "))
na=input("Enter Item name : ")
p=float(input("Enter price : "))
#NEW STYLE
#q="insert into stationary(i_no,i_name,price)
values({},'{}',{})".format(no,na,p)
#q="insert into stationary values({},'{}',{})".format(no,na,p)
# OLD STYLE
#q="insert into stationary(i_no,i_name,price)
values(%s,'%s',%s)"%(no,na,p)
q="insert into stationary values(%s,'%s',%s)"%(no,na,p)
cur.execute(q)
con.commit()
ch=input("Do you want to enter more record..... ")
if ch not in('y','Y'):
break

def Update():
import mysql.connector
con=mysql.connector.connect(host="localhost",
user="root",
password="mysql25",
database='Restaurant'
)
cur=con.cursor()
print("......TO UPDATE RECORD......")
no=int(input("Enter item no. : "))
p=float(input("Enter price : "))
cur.execute("update stationary set price={} where i_no={}".format(p,no))
con.commit()

def Delete():
import mysql.connector
con=mysql.connector.connect(host="localhost",
user="root",
password="mysql25",
database='Restaurant'
)
cur=con.cursor()
print("......TO DELETE RECORD......")
no=int(input("Enter item no. : "))
cur.execute("delete from stationary where i_no={}".format(no))
con.commit()

def Display():
import mysql.connector
con=mysql.connector.connect(host="localhost",
user="root",
password="mysql25",
database='Restaurant'
)
cur=con.cursor()
cur.execute("select * from stationary")
d3=cur.fetchall()
for i in d3:
print(i)

def Search():
import mysql.connector
con=mysql.connector.connect(host="localhost",
user="root",
password="mysql25",
database='Restaurant'
)
cur=con.cursor()
print(".........SEARCH RECORDS IN TABLE.......")
no=int(input("Enter item no. to be searched ..."))
cur.execute("select * from stationary where i_no={}".format(no))
d3=cur.fetchall()
for i in d3:
print(i)

def menu():
while True:
print("Press 1. For creating database")
print("Press 2. For creating table")
print("Press 3. For inserting records")
print("Press 4. For display records")
print("Press 5. For search records")
print("Press 6. For updating records")
print("Press 7. For deleting records ")
choice=int(input("Enter your choice : "))
if choice==1:
createDatabase()
elif choice==2:
createTable()
elif choice==3:
Insert()
elif choice==4:
Display()
elif choice==5:
Search()
elif choice==6:
Update()
elif choice==7:
Delete()
else:
print("Please enter valid choice ")
ch=input("Do you want to continue with MAIN MENU..... ")
if ch not in('y','Y'):
break

menu()

OUTPUT:

You might also like