-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_with_python.py
More file actions
77 lines (63 loc) · 1.83 KB
/
mysql_with_python.py
File metadata and controls
77 lines (63 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from tabulate import tabulate
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',password='Bhar2002',database='python_db')
# if con:
# print("connected")
# else:
# print("error")
def insert(name,age,city):
res=con.cursor()
sql="insert into users (name, age, city) values (%s,%s,%s)"
user=(name,age,city)
res.execute(sql,user)
con.commit()
print("Data insert success")
def update(name,age,city,id):
res=con.cursor()
sql="update users set name=%s, age=%s, city=%s where id=%s"
user=(name,age,city,id)
res.execute(sql,user)
con.commit()
print("Data update success")
def select():
res=con.cursor()
sql="SELECT ID, NAME, AGE, CITY from users"
res.execute(sql)
result=res.fetchall()
# result=res.fetchone()
# result=res.fetchmany(2)
print(tabulate(result,headers=["ID","NAME","AGE","CITY"]))
def delete(id):
res=con.cursor()
sql="delete from users where id=%s"
user=(id,)
res.execute(sql,user)
con.commit()
print("Data Delete success")
while True:
print('1. Insert Data')
print('2. Update Data')
print('3. Select Data')
print('4. Delete Data')
print('5. Exit')
u_choice=int(input("Enter Your Choice : "))
if u_choice==1:
name=input("Enter the Name : ")
age=input("Enter the Age : ")
city=input("Enter your City : ")
insert (name,age,city)
elif u_choice==2:
id=input('Enter the ID :')
name=input('Enter the Name :')
age=input("Enter the Age : ")
city=input('Enter your City :')
update (name,age,city,id)
elif u_choice==3:
select()
elif u_choice==4:
id=input("Enter the ID Delete :")
delete(id)
elif u_choice==5:
quit()
else:
print("Select correct choice, Plase try again")