Python实现通讯录功能
作者:MTbaby 时间:2022-06-28 22:33:26
说实话,第一次写这么长的Python代码,期间遇到了很多问题,但是,最终还是完成了,花了我一天半的时间。
该程序实现了用户的增,删,改,查,主要用到sqlite3模块。对于该模块的知识点,请查看我的另一篇文章SQLite-Python
如有更好的建议,请私信,本人将不胜荣幸,让我们一起来学习Python!
#-*- coding:utf-8 -*-
import sqlite3
#打开本地数据库用于存储用户信息
conn = sqlite3.connect('mysql_person.db')
#在该数据库下创建表,创建表的这段代码在第一次执行后需要注释掉,否则再次执行程序会一直提示:该表已存在
conn.execute('''CREATE TABLE MT
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
print "Table created successfully";
conn.close()
#增加用户信息
def insert():
ID = input('请输入用户ID:\n')
NAME = raw_input('请输入用户昵称:\n')
AGE = input('请输入年龄:\n')
ADDRESS = raw_input('请输入用户地址:\n')
SALARY = input('请输入用户薪水:\n')
sql1 = 'insert into MT(ID,NAME,AGE,ADDRESS,SALARY)'
sql1 += 'values("%d","%s","%d","%s","%d");'%(ID,NAME,AGE,ADDRESS,SALARY)
conn.execute(sql1)
conn.commit()
print "Records insert successfully"
#删除用户信息
def delete():
name = raw_input("请输入所要删除的联系人姓名:")
cursor = conn.execute("SELECT name from MT where name = '%s';"%name)
for row in cursor:
if name == row[0]:
conn.execute("DELETE from MT where name = '%s';"%name)
conn.commit()
print "Records delete successfully"
break
else:
print "sorry,不存在该用户"
#修改用户信息
def modify():
name = raw_input("请输入要修改用户的姓名:")
print search()
sql4 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name
cursor = conn.execute(sql4)
x = raw_input("请输入要修改用户的新地址:")
y = input("请输入要修改用户的新年龄:")
z = input("请输入要修改用户的新薪水:")
sql3 = "UPDATE MT set address = '%s',age = '%d',\
salary = '%d' where name = '%s';"%(x,y,z,name)
conn.execute(sql3)
conn.commit()
print "修改成功"
sql5 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name
cursor = conn.execute(sql5)
for row in cursor:
print "ID = ", row[0]
print "NAME = ", row[1]
print "AGE = ",row[2]
print "ADDRESS = ", row[3]
print "SALARY = ", row[4], "\n"
#查询用户信息
conn = sqlite3.connect('mysql_person.db')
def search():
conn = sqlite3.connect('mysql_person.db')
name = raw_input('请输入要查询的用户姓名')
sql2 = "SELECT id,name,age, address, salary from MT where name= '%s';" % (name)
cursor = conn.execute(sql2)
for row in cursor:
print "ID = ", row[0]
print "NAME = ", row[1]
print "AGE = ",row[2]
print "ADDRESS = ", row[3]
print "SALARY = ", row[4], "\n"
break
else:
print "sorry,没有该用户信息"
#显示所有用户信息
def showall():
cursor = conn.execute("SELECT id, age, name, address, salary from MT")
for row in cursor:
print "ID = ", row[0]
print "NAME = ", row[1]
print "AGE = ",row[2]
print "ADDRESS = ", row[3]
print "SALARY = ", row[4], "\n"
print "Operation done successfully";
cursor = conn.execute("select count(*) from MT;")
for row in cursor:
print "一共有%d个用户"%row[0]
def menu():
print '1.新增联系人'
print '2.删除联系人'
print '3.修改联系人'
print '4.查询联系人'
print '5.显示所有联系人'
print '6.退出程序'
print 'What do you want to do?'
while True:
menu()
x = raw_input('请输入您的选择菜单号:')
if x == '1':
insert()
continue
if x == '2':
delete()
continue
if x == '3':
modify()
continue
if x == '4':
search()
continue
if x == '5':
showall()
continue
if x == '6':
print "谢谢使用!"
exit()
continue
else:
print "输入的选项不存在,请重新输入!"
continue
来源:http://blog.csdn.net/MTbaby/article/details/58599864
标签:python,通讯录
0
投稿
猜你喜欢
python适合做数据挖掘吗
2021-03-25 00:42:58
django-allauth入门学习和使用详解
2022-05-24 12:36:09
setInterval 和 setTimeout 会产生内存溢出
2008-03-08 13:10:00
python模块之paramiko实例代码
2022-08-08 08:34:51
JavaScript中的64位加密及解密
2009-12-23 19:10:00
pandas实现一行拆分成多行
2021-09-19 16:22:37
Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
2024-01-17 17:32:18
python的id()函数介绍
2021-12-18 09:17:45
pandas 数据索引与选取的实现方法
2021-07-09 17:37:44
numpy中np.dstack()、np.hstack()、np.vstack()用法
2021-08-27 11:47:42
SpringBoot用多线程批量导入数据库实现方法
2024-01-23 23:03:36
在Python程序中操作文件之isatty()方法的使用教程
2022-01-24 18:11:44
ASP环境中使用QQ纯真版IP数据库QQWry.dat
2010-02-26 13:35:00
Python3 利用face_recognition实现人脸识别的方法
2023-11-05 05:08:02
python批量替换多文件字符串问题详解
2023-05-08 23:48:06
MySQL实现字符串截取的图文教程
2024-01-16 05:44:05
MySQL5.7.21安装与密码图文配置教程
2024-01-14 16:33:10
sqlserver 禁用触发器和启用触发器的语句
2024-01-19 21:38:17
JavaScript中.min.js和.js文件的区别讲解
2024-04-30 09:56:19
MySQL转义字符
2011-06-19 16:06:04