通讯录

用 pymysql 写了一个简单的通讯录。

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
import pymysql


#connection(连接)/ cursor(游标)
conn = pymysql.connect(host='localhost', port=3306, user='root',
passwd='123456', db='homework', charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
class Contacts():
def __init__(self):
self.cursor = conn.cursor()
def add(self):
name = input('姓名:')
tel = input('电话号码:')
addr = input('地址:')
birth = input('出生日期:')
result = self.cursor.execute('insert into tb_contacts
(cname, ctel, caddr, cbirth) values (%s, %s, %s, %s)'
, (name, tel, addr, birth))
print('添加成功' if result == 1 else '添加失败')
conn.commit()

def search(self, name):
self.cursor.execute('select cname 姓名, ctel 电话, caddr 地址,
cbirth 出生日期 from tb_contacts
where cname like (%s)', (name))
result = self.cursor.fetchone()
print(result if result else '没有该联系人')
return result

def delete(self):
name = input('姓名:')
if self.search(name):
result = self.cursor.execute('delete from tb_contacts
where cname=%s', (name,))
t = input('确认删除?(确认请输入: y)')
if t == 'y' or t == 'Y':
print('删除成功' if result == 1 else '删除失败')
conn.commit()
else:
print('取消删除')

def modify(self):
name = input('需要编辑的联系人姓名:')
if self.search(name):
judg = True
while judg:
num = int(input('请输入编辑项:1.姓名 2.电话
3.地址 4.出生日期 5.退出 \n:'))
if num == 1:
m_name = input('姓名:')
result = self.cursor.execute('update tb_contacts
set cname=%s where cname=%s',(m_name, name))
print('已更改' if result ==1 else '更改失败')
conn.commit()
self.search(m_name)
elif num == 2:
m_tel = input('电话:')
result = self.cursor.execute('update tb_contacts
set ctel=%s where cname=%s', (m_tel, name))
print('已更改' if result == 1 else '更改失败')
conn.commit()
self.search(name)
elif num == 3:
m_addr = input('地址:')
result = self.cursor.execute('update tb_contacts
set caddr=%s where cname=%s', (m_addr, name))
print('已更改' if result == 1 else '更改失败')
conn.commit()
self.search(name)
elif num == 4:
m_birth = input('地址:')
result = self.cursor.execute('update tb_contacts
set cbirth=%s where cname=%s', (m_birth, name))
print('已更改' if result == 1 else '更改失败')
conn.commit()
self.search(name)
elif num == 5:
judg = False
else:
print('没有该联系人请选择添加')

def all(self):
self.cursor.execute('select cname 姓名, ctel 电话, caddr 地址,
cbirth 出生日期 from tb_contacts')
result = self.cursor.fetchall()
for a in result:
print(a)
def menu():
print('通讯录:\n1.添加 \n2.查询 \n3.删除 \n4.编辑 \n5.查看所有联系人
\n6.退出 ')
contact = Contacts()
def main():
menu()

try:
with conn.cursor() as cursor:
while True:
put = int(input('请输入相应数字操作:'))
if put == 1:
contact.add()
elif put == 2:
name = input('姓名:')
contact.search(name)
elif put == 3:
contact.delete()
elif put == 4:
contact.modify()
elif put == 5:
contact.all()

elif put == 6:
break
# conn.close()
else:
print('输入无效')

finally:
conn.close()


if __name__ == '__main__':
main()
0%