Skip to content

Commit eade73f

Browse files
committed
'添加了PyMySQL的参考代码'
1 parent 0da68f5 commit eade73f

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

Day36-40/code/contact/main.py

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import pymysql
2+
3+
INSERT_CONTACTER = """
4+
insert into tb_contacter (conname, contel, conemail)
5+
values (%s, %s, %s)
6+
"""
7+
DELETE_CONTACTER = """
8+
delete from tb_contacter where conid=%s
9+
"""
10+
UPDATE_CONTACTER = """
11+
update tb_contacter set conname=%s, contel=%s, conemail=%s
12+
where conid=%s
13+
"""
14+
SELECT_CONTACTERS = """
15+
select conid as id, conname as name, contel as tel, conemail as email
16+
from tb_contacter limit %s offset %s
17+
"""
18+
COUNT_CONTACTERS = """
19+
select count(conid) as total from tb_contacter
20+
"""
21+
22+
23+
def input_contacter_info():
24+
name = input('姓名: ')
25+
tel = input('手机: ')
26+
email = input('邮箱: ')
27+
return name, tel, email
28+
29+
30+
def add_new_contacter(con):
31+
name, tel, email = input_contacter_info()
32+
try:
33+
with con.cursor() as cursor:
34+
if cursor.execute(INSERT_CONTACTER,
35+
(name, tel, email)) == 1:
36+
print('添加联系人成功!')
37+
except pymysql.MySQLError as err:
38+
print(err)
39+
print('添加联系人失败!')
40+
41+
42+
def delete_contacter(con, contacter_id):
43+
try:
44+
with con.cursor() as cursor:
45+
if cursor.execute(DELETE_CONTACTER, (contacter_id, )) == 1:
46+
print('联系人已经删除!')
47+
except pymysql.MySQLError as err:
48+
print(err)
49+
print('删除联系人失败!')
50+
51+
52+
def edit_contacter_info(con, contacter):
53+
name, tel, email = input_contacter_info()
54+
contacter['name'] = name or contacter['name']
55+
contacter['tel'] = tel or contacter['tel']
56+
contacter['email'] = email or contacter['email']
57+
try:
58+
with con.cursor() as cursor:
59+
if cursor.execute(UPDATE_CONTACTER,
60+
(contacter['name'], contacter['tel'],
61+
contacter['email'], contacter['id'])) == 1:
62+
print('联系人信息已经更新!')
63+
except pymysql.MySQLError as err:
64+
print(err)
65+
print('更新联系人信息失败!')
66+
67+
68+
def show_contacter_detail(con, contacter):
69+
print('姓名:', contacter['name'])
70+
print('手机号:', contacter['tel'])
71+
print('邮箱:', contacter['email'])
72+
choice = input('是否编辑联系人信息?(yes|no)')
73+
if choice == 'yes':
74+
edit_contacter_info(con, contacter)
75+
else:
76+
choice = input('是否删除联系人信息?(yes|no)')
77+
if choice == 'yes':
78+
delete_contacter(con, contacter['id'])
79+
80+
81+
def find_all_contacters(con):
82+
page, size = 1, 5
83+
try:
84+
with con.cursor() as cursor:
85+
cursor.execute(COUNT_CONTACTERS)
86+
total = cursor.fetchone()['total']
87+
while True:
88+
cursor.execute(SELECT_CONTACTERS,
89+
(size, (page - 1) * size))
90+
contacters_list = []
91+
for index, row in enumerate(cursor.fetchall()):
92+
contacters_list.append(row)
93+
print('[%d]: %s' % (index, row['name']))
94+
choice = input('是否查看联系人详情?(yes|no)')
95+
if choice.lower() == 'yes':
96+
index = int(input('请输入编号: '))
97+
if 0 <= index < cursor.rowcount:
98+
show_contacter_detail(con, contacters_list[index])
99+
if page * size < total:
100+
choice = input('继续查看下一页?(yes|no)')
101+
if choice.lower() == 'yes':
102+
page += 1
103+
else:
104+
break
105+
else:
106+
print('没有下一页记录啦!')
107+
break
108+
109+
except pymysql.MySQLError as err:
110+
print(err)
111+
112+
113+
def find_contacters_by_name(con):
114+
pass
115+
116+
117+
def find_contacters(con):
118+
while True:
119+
print('1. 查看所有联系人')
120+
print('2. 搜索联系人')
121+
print('3. 退出查找')
122+
choice = int(input('请输入: '))
123+
if choice == 1:
124+
find_all_contacters(con)
125+
elif choice == 2:
126+
find_contacters_by_name(con)
127+
elif choice == 3:
128+
break
129+
130+
131+
def main():
132+
con = pymysql.connect(host='10.7.185.126', port=3306,
133+
user='root', passwd='123456',
134+
db='contact', charset='utf8',
135+
autocommit=True,
136+
cursorclass=pymysql.cursors.DictCursor)
137+
while True:
138+
print('=====通讯录=====')
139+
print('1. 新建联系人')
140+
print('2. 查找联系人')
141+
print('3. 退出系统')
142+
print('===============')
143+
choice = int(input('请选择: '))
144+
if choice == 1:
145+
add_new_contacter(con)
146+
elif choice == 2:
147+
find_contacters(con)
148+
elif choice == 3:
149+
con.close()
150+
print('谢谢使用, 再见!')
151+
break
152+
153+
154+
if __name__ == '__main__':
155+
main()

Day36-40/code/contact/test01.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def main():
2+
list1 = ['orange', 'grape', 'banana', 'waxberry', 'pitaya']
3+
for index, val in enumerate(list1):
4+
print('%d: %s' % (index, val))
5+
6+
7+
if __name__ == '__main__':
8+
main()

Day41-55/shop/static/images/Thumbs.db

-21 KB
Binary file not shown.

0 commit comments

Comments
 (0)