0% found this document useful (0 votes)
23 views9 pages

Ak Python p4

The document outlines a series of practical exercises for programming with Python and MySQL, including connecting to a MySQL database, creating databases and tables, inserting records, and displaying data. It provides code snippets for each task along with expected outputs. Additionally, it includes a simple GUI application using Tkinter for adding student records to a database.

Uploaded by

Sajid Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Ak Python p4

The document outlines a series of practical exercises for programming with Python and MySQL, including connecting to a MySQL database, creating databases and tables, inserting records, and displaying data. It provides code snippets for each task along with expected outputs. Additionally, it includes a simple GUI application using Tkinter for adding student records to a database.

Uploaded by

Sajid Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

NAME- SAJID AHMED Roll no-001

Subject: Programming with Python Date: 23/02/2023


Practical-4
Q.1) Open mysql connector in python and display all the databases.
Code:

Output:

Q.2) Create a database kccollege and again show the databases.


Code:
import mysql.connector as m
db=m.connect(host='localhost',user='root', password='admin')
a=db.cursor()
a.execute("create database hrcollege")
a.execute("show databases")
for i in a :
print(i)
Output:

1
Q.3) Create a table hotel and show the structure item no, name of the dish, cost, type
Code:
import mysql.connector as m
db=m.connect(host='localhost',user='root', password='admin')
a=db.cursor()
a.execute("use hrcollege")
a.execute("create table hotel(item_no int, dish_name varchar(30), cost float, type
varchar(30))")
a.execute("desc hotel")
for i in a :
print(i)
Output:

Q.4) Insert a record into table hotel.


Code:
import mysql.connector as m
db=m.connect(host='localhost',user='root', password='admin')

2
a=db.cursor()
a.execute("use hrcollege")
a.execute("insert into hotel values(1,'noodles',290,'main course' )")
a.execute("select * from hotel")
for i in a :
print(i)
Output:

import mysql.connector as m
db=m.connect(host='localhost',user='root', password='admin')
a=db.cursor()
a.execute('use hrcollege')
f="insert into hotel (item_no, dish_name, cost, type) values(%s,%s,%s,%s)"
v=(2,'pasta',200,'V')
a.execute(f,v)
db.commit()
a.execute("select * from hotel")
r=a.fetchall()
for i in r :
print(i)
Output:

Q.5) Create a table with roll no, name, marks and percentage.
Code:
import mysql.connector as m

3
db=m.connect(host='localhost',user='root', password='root', database='kccollege')
a=db.cursor()
a.execute("create table student (rollno int,name varchar(30), marks float, percentage float)")
a.execute("desc student")
for i in a :
print(i)
Output:

Q.6) Insert into Student table.


Code:
import mysql.connector as m
db=m.connect(host='localhost',user='root', password='root', database='kccollege')
a=db.cursor()
a.execute("insert into student values(1,'Jagruti', 400, 85)")
a.execute("select * from student")
for i in a :
print(i)
Output:

Q7]
Code:
import mysql.connector as m
db=m.connect(host='localhost',user='root', password='root',database="kccollege")

4
a=db.cursor()
print("1.ADD")
print("2.MODIFY")
print("3.DELETE")
print("4.DISPLAY")
s=int(input("Enter choice(1-4):"))
if s==1:
r=int(input("Roll No.:"))
n=input("Name:")
m=int(input("Marks:"))
p=float(input("Percentage:"))
sq="Insert into student1(rollno,name,marks,percentage) values(%s,%s,%s,%s)"
val=(r,n,m,p)
a.execute(sq,val)
db.commit()
elif s==2:
r=int(input("Enter Roll No to be updated.:"))
n=input("enter Name:")
m=int(input("enter Marks:"))
p=float(input("enter Percentage:"))
sq="update student1 set name=%s,marks=%s,percentage=%s where rollno=%s;"
val=(n,m,p,r)
a.execute(sq,val)
db.commit()
elif s==3:
r=int(input("Enter Roll No to be deleted.:"))
sq="delete from student1 where rollno=%s"
val=(r,)

5
a.execute(sq,val)
db.commit()
elif s==4:
r=int(input("Enter Roll No to be displayed.:"))
sq="select * from student1 where rollno=%s"
val=(r,)
a.execute(sq,val)

else:
print("Wrong choice")
for i in a:
print(i)

Output:

Q8]
Code:

6
import tkinter as t
w=t.Tk()
import mysql.connector as m
db=m.connect(host="localhost",user="root",password="root",database="kccollege")
v=db.cursor()
w.geometry("400x400")
l1=t.Label(text="Roll no:")
l1.grid(row=1,column=1)
e1=t.Entry()
e1.grid(row=1,column=2)
l2=t.Label(text="Name")
e2=t.Entry()
e2.grid(row=2,column=2)
l2.grid(row=2,column=1)
l3=t.Label(text="Marks")
l3.grid(row=3,column=1)
e3=t.Entry()
e3.grid(row=3,column=2)
l4=t.Label(text="Percentage")
l4.grid(row=4,column=1)
e4=t.Entry()
e4.grid(row=4,column=2)

def sq():
f1=int(e1.get())
f2=e2.get()
f3=int(e3.get())
f4=float(e4.get())

7
a="insert into student1(rollno,name,marks,percentage) values(%s,%s,%s,%s)"
val=(f1,f2,f3,f4)
v.execute(a,val)
db.commit()
b1=t.Button(text="add",fg="blue",command=sq)
b1.grid(row=5,column=2)

w.mainloop()
Output:

8
9

You might also like