-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (78 loc) · 2.5 KB
/
main.py
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
78
79
80
81
82
83
84
85
from asyncio.windows_events import NULL
from sqlite3 import connect
import mysql.connector
connect = mysql.connector.connect(
host = "127.0.0.1",
user = "root",
password = "",
database = "db_crud_python",
port = "3307"
)
myCursor = connect.cursor()
then = True
while then:
print("\nCRUD User")
print("1. Lihat user")
print("2. Tambah user")
print("3. Ubah user")
print("4. Hapus user")
print("5. Keluar\n")
a = input("Pilih menu : ")
option = None
if a != "":
option = int(a)
else:
option = None
if (option == 1):
myCursor.execute("SELECT * FROM user")
myResult = myCursor.fetchall()
print("\n(id, nama, email, no hp)")
for x in myResult:
print(x)
elif(option == 2):
nama = input("Nama : ")
email = input("Email : ")
no_hp = input("No HP : ")
sql = "INSERT INTO `user` (`nama`, `email`, `no_hp`) VALUES (%s, %s, %s)"
val = (nama, email, no_hp)
myCursor.execute(sql, val)
connect.commit()
print(myCursor.rowcount, "data berhasil ditambah")
elif(option == 3):
id = input("\nID user : ")
myCursor.execute("SELECT * FROM user WHERE id = "+id+" LIMIT 1")
myResult = myCursor.fetchall()
user = None
for x in myResult:
user = x
if (user != None):
nama = input("Nama ("+user[1]+") :") or user[1]
email = input("Email ("+user[2]+") :") or user[2]
no_hp = input("No HP ("+str(user[3])+") :") or user[3]
sql = "UPDATE user SET nama=%s,email=%s,no_hp=%s WHERE id=%s"
val = (nama, email, no_hp, id)
myCursor.execute(sql, val)
connect.commit()
print(myCursor.rowcount, "data berhasil diubah")
else:
print("ID user tidak ditemukan")
elif(option == 4):
id = input("\nID user : ")
myCursor.execute("SELECT * FROM user WHERE id ="+id+" LIMIT 1")
myResult = myCursor.fetchall()
user = None
for x in myResult:
user = x
if (user != None):
print("Hapus data : ",user)
sql = "DELETE FROM user WHERE id ="+id
myCursor.execute(sql)
connect.commit()
print("\nData berhasil dihapus")
else:
print("ID user tidak ditemukan")
elif(option == 5):
print("\nBye :p\n")
then = False
else:
print("\nMenu belum dibuat :)")