- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
Latest commit
138 lines (118 loc) · 3.74 KB
/
Copy pathmain.py
File metadata and controls
138 lines (118 loc) · 3.74 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
importpymysql# 连接Mysql
fromsysimportexit# 退出
fromosimportsystem# 清空控制台
defconn(): # 连接Mysql的函数
db=pymysql.connect(host='localhost', user='root', passwd='233233', db='名片', charset='utf8')
returndb
defclose(self): # 关闭连接的函数
self.cursor.close() # 关闭游标
self.db.close() # 关闭连接
deff1(): # 实现功能1
system('cls') # 清空控制台
print("**"*20)
db=conn()
try:
new_name=input("请输入姓名:")
new_sex=input("请输入性别:")
new_age=int(input("请输入年龄:"))
cursor=db.cursor() # 创建游标
sql_insert="insert into Namecard values('%s','%s',%d)"% (new_name, new_sex, new_age) # T-SQL语句
cursor.execute(sql_insert) # 执行语句
db.commit() # 保存
print("添加成功!")
exceptExceptionase:
db.rollback() # 出现异常回滚
print("失败!", e)
db.close() # 关闭连接
xuan_ze() # 继续运行
deff2(): # 实现功能2
system('cls')
print("**"*20)
db=conn()
try:
cname=input("请输入删除人姓名:")
cursor=db.cursor()
sql_delete="delete from Namecard where 姓名='%s'"%cname# T-SQL语句
cursor.execute(sql_delete)
db.commit()
exceptExceptionase:
db.rollback()
print("失败!", e)
db.close()
xuan_ze()
deff3(): # 实现功能3
system('cls')
print("**"*20)
db=conn()
try:
cname=input("请输入修改姓名:")
new_name=input("请输入姓名:")
new_sex=input("请输入性别:")
new_age=int(input("请输入年龄:"))
cursor=db.cursor()
sql="update Namecard set 姓名='%s',性别='%s',年龄=%d where 姓名='%s'"% (new_name, new_sex, new_age, cname)
cursor.execute(sql)
db.commit()
exceptExceptionase:
db.rollback()
print("失败!", e)
db.close()
xuan_ze()
deff4(): # 实现功能4
system('cls')
print("**"*20)
db=conn()
try:
cname=input("请输入查询姓名:")
cursor=db.cursor()
sql="select * from Namecard where 姓名='%s'"%cname# T-SQL语句 查询时不需要 commit()
cursor.execute(sql)
result=cursor.fetchone()
print("姓名:", result[0], "\t性别:", result[1], "\t年龄:", result[2])
exceptExceptionase:
print("失败!", e)
db.close()
xuan_ze()
deff5(): # 实现功能5
system('cls')
print("**"*20)
db=conn()
try:
print("姓名\t性别\t年龄")
cursor=db.cursor()
sql="select * from Namecard"# T-SQL语句
cursor.execute(sql)
result=cursor.fetchall()
fordatainresult:
print(data[0], "\t", data[1], "\t", data[2])
exceptExceptionase:
print("失败!", e)
db.close()
xuan_ze()
deff6(): # 实现功能6
exit() # 退出函数
defxuan_ze(): # 实现选择
print("**"*20)
print("1.添加名片", "\t"*3, "2.删除名片", "\t"*3, "3.修改名片")
print("4.查询名片", "\t"*3, "5.获取所有名片信息", "\t"*2, "6.退出系统")
# 打印
c=int(input("选择:"))
# 获取选项
ifc==1:
f1()
elifc==2:
f2()
elifc==3:
f3()
elifc==4:
f4()
elifc==5:
f5()
elifc==6:
f6()
else:
print("请选择数字1~6")
xuan_ze()
# 判断选项
xuan_ze()
# 执行