Pymysql Emp Table
Pymysql Emp Table
import pymysql
db1=pymysql.connect(host='localhost',user='root',password='1234')
cur=db1.cursor()
cur.execute('use abhinavmohak;')
cur.execute('select * from emp;')
result=cur.fetchall()
from tabulate import tabulate
headers = [desc[0] for desc in cur.description]
print(tabulate(result, headers=headers, tablefmt="pretty"))
print('''1.insert new records
2.delete record
3.update record
4.search in table
5.view table
6.exit program''')
while True:
ch=int(input('enter your choice '))
if ch==1:
sno=int(input('enter sno '))
name=input('enter name ')
date=input('enter date of join ')
sal=int(input('enter salary '))
post=input('enter post')
query="insert into emp values('%d','%s','%s','%d','%s');"%(sno,name,date,sal,post)
cur.execute(query)
db1.commit()
elif ch==2:
sno=int(input('enter sno '))
query="delete from emp where sno='%d'"%(sno)
cur.execute(query)
db1.commit()
elif ch==3:
print('''1.update name
2.update salary
3.update post
4.do other operations''')
while True:
choice=int(input('enter what is to be changed '))
if choice==1:
sno=int(input('enter sno' ))
name=input('enter new name ')
query="update emp set name='%s' where sno='%d'"%(name,sno)
cur.execute(query)
db1.commit()
elif choice==2:
sno=int(input('enter sno '))
sal=int(input('enter new salary '))
query="update emp set salary='%d' where sno='%d'"%(sal,sno)
elif choice==3:
sno=int(input('enter sno '))
post=input('enter new post ')
query="update emp set post='%s' where sno='%d'"%(post,sno)
elif choice==4:
break
else:
print('invalid choice')
elif ch==4:
name=input('enter the name you want to search in the table ')
query='select name from emp;'
cur.execute(query)
names=cur.fetchall()
i=0
for k in names:
if (k[i]==name):
print('yes the name is present ')
elif ch==5:
cur.execute('select * from emp;')
https://pastebin.com 1/2
8/7/24, 9:06 PM Pastebin.com - #1 paste tool since 2002!
result=cur.fetchall()
from tabulate import tabulate
headers = [desc[0] for desc in cur.description]
print(tabulate(result, headers=headers, tablefmt="pretty"))
elif ch==6:
print('BYE')
break
https://pastebin.com 2/2