-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmian.py
105 lines (83 loc) · 3.18 KB
/
mian.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pymysql
import prettytable as pt
import os
# 登入資料庫
id = input("請輸入資料庫帳號: ")
pw = input("請輸入資料庫密碼: ")
link = pymysql.connect(
host="teaching-db.bo-yuan.net",
user=id,
passwd=pw,
db="AI09_02",
charset="utf8",
port=3306
)
# 取得指令操作變數
cur = link.cursor()
# 清除螢幕指令
def clear(): return os.system('cls')
# 列出所有資料,由於更新和刪除的指令也會需要將會員資料表印出,故寫成function來重複使用
def show_all_data():
clear()
# 搜尋資料庫找出會員列表
cur.execute("SELECT * FROM `member`")
data = cur.fetchall()
# 先準備會員列表表格
show_all = pt.PrettyTable(["編號", "姓名", "生日", "地址"])
# 製作表格
for item in data:
show_all.add_row([item[0], item[1], item[2], item[3]])
# 印出所有資料
print(show_all)
return data
# 登入成功後印出選單
while True:
command = input(
"(0) 離開程式\n(1) 顯示會員列表\n(2) 新增會員資料\n(3) 更新會員資料\n(4) 刪除會員資料\n指令: ")
if command == "1":
# 搜尋資料庫找出會員列表
show_all_data()
elif command == "2":
clear()
name = input("請輸入會員姓名: ")
birthday = input("請輸入會員生日: ")
address = input("請輸入會員地址: ")
cur.execute("INSERT INTO `member`(`name`, `birthday`, `address`) VALUES (%s, %s, %s)", [
name, birthday, address])
link.commit()
# 印出最新一筆新增資料
clear()
newest_id = cur.lastrowid
cur.execute("SELECT * FROM `member` WHERE `id` = %s", newest_id)
print(cur.fetchone(), "新增成功")
elif command == "3":
data = show_all_data()
update_id = input("請選擇你要修改的資料編號: ")
# check all_ids: 有在all_ids列表中的才執行修改、收input值,其他輸入則無動作
all_ids = [str(item[0]) for item in data]
if update_id in all_ids:
name = input("請輸入會員姓名: ")
birthday = input("請輸入會員生日: ")
address = input("請輸入會員地址: ")
cur.execute("UPDATE `member` SET `name`=%s,`birthday`=%s,`address`=%s WHERE `id`=%s", [
name, birthday, address, int(update_id)])
link.commit()
clear()
print(f"編號{update_id}資料已修改")
else:
clear()
elif command == "4":
data = show_all_data()
delete_id = input("請選擇你要刪除的資料編號: ")
# check all_ids: 有在all_ids列表中的才執行刪除,其他輸入值無動作
all_ids = [str(item[0]) for item in data]
if delete_id in all_ids:
cur.execute("DELETE FROM `member` WHERE `id`=%s", int(delete_id))
link.commit()
clear()
print(f"編號{delete_id}資料已刪除")
else:
clear()
elif command == "0":
break
# Here's my GitHub repository link: https://github.com/linbeta/simple-mysql-text-interface.git