开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

jackfrued/python2005

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python2005
/
example03.py
python2005
/
example03.py
example03.py 5.45 KB
一键复制 编辑 原始数据 按行查看 历史
jackfrued 提交于 2021年02月09日 13:07 +08:00 . 更新了部分代码
"""
通讯录
"""
import MySQLdb
conn = None
def ensure_connection():
global conn
if not conn:
conn = MySQLdb.connect(host='47.104.31.138', port=3306,
user='hellokitty', password='Hellokitty.618',
database='python2005', charset='utf8mb4')
return conn
def add_contact():
"""添加联系人"""
ensure_connection()
name = input('姓名: ').strip()
tel = input('电话: ').strip()
email = input('邮箱: ').strip()
company = input('公司: ').strip()
memo = input('备注: ').strip()
try:
with conn.cursor() as cursor:
affected_rows = cursor.execute(
'insert into tb_contact (name, tel, email, company, memo) '
'values (%s, %s, %s, %s, %s)',
(name, tel, email, company, memo)
)
if affected_rows == 1:
print('新增联系人成功!')
conn.commit()
except MySQLdb.MySQLError as err:
print(err)
conn.rollback()
def show_all_contacts():
"""显示所有联系人"""
ensure_connection()
with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
cursor.execute('select id, name from tb_contact')
display_contacts(cursor.fetchall())
def display_contacts(all_rows):
"""显示联系人名字清单"""
ensure_connection()
for index, row_dict in enumerate(all_rows):
print(f'[{index + 1}]: {row_dict["name"]}')
try:
choice = int(input('输入编号查看联系人详情: '))
if 0 < choice <= len(all_rows):
contact_id = all_rows[choice - 1]['id']
show_contact_detail(contact_id)
except ValueError:
pass
def delete_contact(contact_id):
"""删除联系人"""
ensure_connection()
try:
with conn.cursor() as cursor:
affected_rows = cursor.execute(
'delete from tb_contact where id=%s', (contact_id, )
)
if affected_rows == 1:
print('联系人信息已删除!')
conn.commit()
except MySQLdb.MySQLError as err:
print(err)
conn.rollback()
def edit_contact(contact_id, contact_dict):
"""编辑联系人"""
ensure_connection()
name = input('姓名: ').strip() or contact_dict['name']
tel = input('电话: ').strip() or contact_dict['tel']
email = input('邮箱: ').strip() or contact_dict['email']
company = input('公司: ').strip() or contact_dict['company']
memo = input('备注: ').strip() or contact_dict['memo']
try:
with conn.cursor() as cursor:
affected_rows = cursor.execute(
'update tb_contact set name=%s, tel=%s, email=%s, company=%s, memo=%s '
'where id=%s',
(name, tel, email, company, memo, contact_id)
)
if affected_rows == 1:
print('联系人信息已修改!')
conn.commit()
except MySQLdb.MySQLError as err:
print(err)
conn.rollback()
def show_contact_detail(contact_id):
"""显示联系人的详情"""
ensure_connection()
with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
cursor.execute(
'select name, tel, email, company, memo from tb_contact where id=%s',
(contact_id, )
)
contact_dict = cursor.fetchone()
print(f'\n姓名:{contact_dict["name"]}')
print(f'电话:{contact_dict["tel"]}')
print(f'邮箱:{contact_dict["email"]}')
print(f'公司:{contact_dict["company"]}')
print(f'备注:{contact_dict["memo"]}', end='\n\n')
print('1. 删除联系人')
print('2. 编辑联系人')
try:
choice = int(input('请选择: '))
if choice == 1:
confirm = input(f'确定要删除联系人{contact_dict["name"]}的信息?(yes/no)')
if confirm.strip().lower() == 'yes':
delete_contact(contact_id)
elif choice == 2:
edit_contact(contact_id, contact_dict)
except ValueError:
pass
def search_contacts():
"""搜索联系人"""
ensure_connection()
keyword = input('关键词: ')
keyword = f'%{keyword}%'
with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
cursor.execute(
'select id, name from tb_contact '
'where name like %s or company like %s or memo like %s',
(keyword, keyword, keyword)
)
display_contacts(cursor.fetchall())
def list_contacts():
print('1. 查看所有联系人')
print('2. 搜索联系人')
print('3. 返回')
choice = 0
while choice <= 0 or choice > 3:
try:
choice = int(input('请选择: '))
except ValueError:
pass
if choice == 1:
show_all_contacts()
elif choice == 2:
search_contacts()
def main():
print('欢迎使用通讯录程序'.center(20, '='))
while True:
print('1. 新增联系人')
print('2. 查看联系人')
print('3. 退出程序')
choice = 0
while choice <= 0 or choice > 3:
try:
choice = int(input('请选择: '))
except ValueError:
pass
if choice == 1:
add_contact()
elif choice == 2:
list_contacts()
else:
print('程序结束'.center(20, '='))
break
if __name__ == '__main__':
main()
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/jackfrued/python2005.git
git@gitee.com:jackfrued/python2005.git
jackfrued
python2005
python2005
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /