当前位置:
首页 > Python基础教程 >
-
python实现简易通讯录修改版
这篇文章主要为大家详细介绍了python实现简易通讯录的修改版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
描述:
上一篇博客我写了一个简单的通讯录,但是还是觉得不够完美:
需要输入ID,虽然ID是主键,但是没有实现自增功能;
忘记加电话号码了;
如果插入用户名相同,则后续的查、改、删功能受到影响;
所以,我这个修改版主要修正了以上三点缺陷。具体如下
将用户ID设置为自增字段,即就是不用输入ID,程序会自动帮你将插入的用户进行排序并附上ID,默认从1开始;SQLite实现主键自增功能,请查看SQLiteAUTOINCREMENT/自动递增这篇博客。
创建表时增加了电话号码字段;
插入的用户名不能重复;
好了,废话不多说,附上新代码,哈哈~~~
#-*- coding:utf-8 -*-
import sqlite3
#打开本地数据库用于存储用户信息
conn = sqlite3.connect('mysql_person.db')
#在该数据库下创建表,创建表的这段代码在第一次执行后需要注释掉,否则再次执行程序会一直提示:该表已存在
conn.execute ('''CREATE TABLE MT_table1(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL,
PhoneNumber INT NOT NULL);''')
print "Table created successfully";
#增加用户信息
def insert():
USER_NAME = raw_input('请输入用户昵称:')
cursor = conn.execute("SELECT name from MT_table1 where name = '%s';"%USER_NAME)
for row in cursor:
if USER_NAME == row[0]:
print "sorry,改用户名已存在,请重新输入用户名"
break
else:
AGE = input('请输入年龄:')
ADDRESS = raw_input('请输入用户地址:')
SALARY = input('请输入用户薪水:')
PhoneNumber = input("请输入联系方式:")
#为避免代码太长,以下我用字符串的连接特性将代码缩短
sql1 = 'INSERT INTO MT_table1(NAME,AGE,ADDRESS,SALARY,PhoneNumber)'
sql1 += 'VALUES("%s","%d","%s","%d","%d");'%(USER_NAME,AGE,ADDRESS,SALARY,PhoneNumber)
conn.execute(sql1)
conn.commit()
print "Records insert successfully"
#删除用户信息
def delete():
delete_name = raw_input("请输入所要删除的联系人姓名:")
cursor = conn.execute("SELECT name from MT_table1 where name = '%s';"%delete_name)
for row in cursor:
if delete_name == row[0]:
conn.execute("DELETE from MT_table1 where name = '%s';"%delete_name)
conn.commit()
print "Records delete successfully"
break
else:
print "sorry,不存在该用户"
#修改用户信息
def modify():
update_name = raw_input("请输入要修改用户的姓名:")
sql6 = "SELECT name from MT_table1 where name = '%s';"%update_name
cursor = conn.execute(sql6)
for row in cursor:
if update_name == row[0]:
New_addr = raw_input("请输入要修改用户的新地址:")
New_age = input("请输入要修改用户的新年龄:")
New_salary = input("请输入要修改用户的新薪水:")
New_num = input("输入要修改用户的新号码:")
sql3 = "UPDATE MT_table1 set address = '%s',age = '%d',salary = '%d',PhoneNumber = '%d' where \
name = '%s';"%(New_addr,New_age,New_salary,New_num,update_name)
conn.execute(sql3)
conn.commit()
print "修改成功"
sql5 = "SELECT id, name, age,address, salary ,PhoneNumber from MT_table1 where name = '%s';"%update_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]
print "PhoneNumber = ",row[5], "\n"
break
else:
print "sorry,不存在该用户信息"
#查询用户信息
def search():
conn = sqlite3.connect('mysql_person.db')
search_name = raw_input('请输入要查询的用户姓名')
sql2 = "SELECT id,name,age, address, salary ,PhoneNumber from MT_table1 where name= '%s';" % (search_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]
print "PhoneNumber = ",row[5], "\n"
break
else:
print "sorry,没有该用户信息"
#显示所有用户信息
def showall():
cursor = conn.execute("SELECT id, name,age, address, salary ,PhoneNumber from MT_table1")
for row in cursor:
print "ID = ", row[0]
print "NAME = ", row[1]
print "AGE = ",row[2]
print "ADDRESS = ", row[3]
print "SALARY = ", row[4]
print "PhoneNumber = ",row[5], "\n"
print "Operation done successfully";
cursor = conn.execute("select count(*) from MT_table1;")
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
复制免费讲解AI专家
#-*- coding:utf-8 -*-
import sqlite3
#打开本地数据库用于存储用户信息
conn = sqlite3.connect('mysql_person.db')
#在该数据库下创建表,创建表的这段代码在第一次执行后需要注释掉,否则再次执行程序会一直提示:该表已存在
conn.execute ('''CREATE TABLE MT_table1(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL,
PhoneNumber INT NOT NULL);''')
print "Table created successfully";
#增加用户信息
def insert():
USER_NAME = raw_input('请输入用户昵称:')
cursor = conn.execute("SELECT name from MT_table1 where name = '%s';"%USER_NAME)
for row in cursor:
if USER_NAME == row[0]:
print "sorry,改用户名已存在,请重新输入用户名"
break
else:
AGE = input('请输入年龄:')
ADDRESS = raw_input('请输入用户地址:')
SALARY = input('请输入用户薪水:')
PhoneNumber = input("请输入联系方式:")
#为避免代码太长,以下我用字符串的连接特性将代码缩短
sql1 = 'INSERT INTO MT_table1(NAME,AGE,ADDRESS,SALARY,PhoneNumber)'
sql1 += 'VALUES("%s","%d","%s","%d","%d");'%(USER_NAME,AGE,ADDRESS,SALARY,PhoneNumber)
conn.execute(sql1)
conn.commit()
print "Records insert successfully"
#删除用户信息
def delete():
delete_name = raw_input("请输入所要删除的联系人姓名:")
cursor = conn.execute("SELECT name from MT_table1 where name = '%s';"%delete_name)
for row in cursor:
if delete_name == row[0]:
conn.execute("DELETE from MT_table1 where name = '%s';"%delete_name)
conn.commit()
print "Records delete successfully"
break
else:
print "sorry,不存在该用户"
#修改用户信息
def modify():
update_name = raw_input("请输入要修改用户的姓名:")
sql6 = "SELECT name from MT_table1 where name = '%s';"%update_name
cursor = conn.execute(sql6)
for row in cursor:
if update_name == row[0]:
New_addr = raw_input("请输入要修改用户的新地址:")
New_age = input("请输入要修改用户的新年龄:")
New_salary = input("请输入要修改用户的新薪水:")
New_num = input("输入要修改用户的新号码:")
sql3 = "UPDATE MT_table1 set address = '%s',age = '%d',salary = '%d',PhoneNumber = '%d' where \
name = '%s';"%(New_addr,New_age,New_salary,New_num,update_name)
conn.execute(sql3)
conn.commit()
print "修改成功"
sql5 = "SELECT id, name, age,address, salary ,PhoneNumber from MT_table1 where name = '%s';"%update_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]
print "PhoneNumber = ",row[5], "\n"
break
else:
print "sorry,不存在该用户信息"
#查询用户信息
def search():
conn = sqlite3.connect('mysql_person.db')
search_name = raw_input('请输入要查询的用户姓名')
sql2 = "SELECT id,name,age, address, salary ,PhoneNumber from MT_table1 where name= '%s';" % (search_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]
print "PhoneNumber = ",row[5], "\n"
break
else:
print "sorry,没有该用户信息"
#显示所有用户信息
def showall():
cursor = conn.execute("SELECT id, name,age, address, salary ,PhoneNumber from MT_table1")
for row in cursor:
print "ID = ", row[0]
print "NAME = ", row[1]
print "AGE = ",row[2]
print "ADDRESS = ", row[3]
print "SALARY = ", row[4]
print "PhoneNumber = ",row[5], "\n"
print "Operation done successfully";
cursor = conn.execute("select count(*) from MT_table1;")
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/59105813
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
开启SQLSERVER数据库缓存依赖优化网站性能
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比