Python怎么编写通讯录通过数据库存储实现模糊查询功能
发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,这篇"Python怎么编写通讯录通过数据库存储实现模糊查询功能"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,
千家信息网最后更新 2025年02月24日Python怎么编写通讯录通过数据库存储实现模糊查询功能
这篇"Python怎么编写通讯录通过数据库存储实现模糊查询功能"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"Python怎么编写通讯录通过数据库存储实现模糊查询功能"文章吧。
1.要求
数据库存储通讯录,要求按姓名/电话号码查询,查询条件只有一个输入入口,自动识别输入的是姓名还是号码,允许模糊查询。
2.实现功能
可通过输入指令进行操作。
(1)首先输入"add",可以对通讯录进行添加联系人信息。
sql1 ='insert into TA(ID,NAME,AGE,ADDRESS,TELENUMBER)'sql1 += 'values("%d","%s","%d","%s","%s");' % (ID,name, age, address, telenumber)conn.execute(sql1)conn.commit() # 提交,否则无法保存
(2)输入"delete",可以删除指定的联系人信息。
输入姓名删除:
cursor = c.execute( "SELECT name from TA where name = '%s';" %i)
输入电话号码删除:
cursor = c.execute( "SELECT name from TA where telenumber= '%s';" % i)
(3)输入"search",可以输入联系人或者电话号码,查询联系人信息,这里实现了模糊查询以及精确查询。
输入姓名查询:
sql1 = "SELECT id,name,age, address, telenumber from TA where telenumber like '%" + i +"%'"cursor = c.execute(sql1)
输入电话号码查询:
sql1= "SELECT id,name,age, address, telenumber from TA where name like '%" +i+"%'"cursor = c.execute(sql1)
(4)输入"searchall",查询全部联系人信息。
cursor = c.execute( "SELECT id, name, age, address, telenumber from TA" )
3.数据库sqlite3
Python自带一个轻量级的关系型数据库sqlite。这一数据库使用SQL语言。sqlite作为后端数据库,可以搭配Python建网站,或者制作有数据存储需求的工具。sqlLite还在其它领域有广泛的应用,比如HTML5和移动端。Python标准库中的sqlite3提供该数据库的接口。因此此次使用了sqlite3数据库存储通讯录的联系人信息。
源码:
import sqlite3import re#打开本地数据库用于存储用户信息conn = sqlite3.connect('mysql_telephone_book.db')c = conn.cursor()#在该数据库下创建表,创建表的这段代码在第一次执行后需要注释掉,否则再次执行程序会一直提示:该表已存在'''c.execute("CREATE TABLE TA (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), TELENUMBER TEXT);")'''conn.commit()#提交当前的事务#增加用户信息def insert(): global conn c = conn.cursor() ID=int(input("请输入id号:")) name=input("请输入姓名:") age=int(input("请输入年龄:")) address=input("请输入地址:") telenumber=input("请输入电话号码:") sql1 = 'insert into TA(ID,NAME,AGE,ADDRESS,TELENUMBER)' sql1 += 'values("%d","%s","%d","%s","%s");' % (ID,name, age, address, telenumber) conn.execute(sql1) conn.commit()#提交,否则无法保存 print("提交成功!!")#删除用户信息def delete(): global conn c=conn.cursor() i = input("请输入所要删除的联系人姓名或电话号码:") if len(i) < 11: cursor = c.execute("SELECT name from TA where name = '%s';"%i) for row in cursor: if i == row[0]: c.execute("DELETE from TA where name ='%s';"%i) conn.commit() print("成功删除联系人信息!!") break else: print("该联系人不存在!!") else : cursor = c.execute("SELECT name from TA where telenumber= '%s';" % i) for row in cursor: if i == row[0]: c.execute("DELETE from TA where telenumber ='%s';" % i) conn.commit() print("成功删除联系人信息!!") break else: print("该电话号码错误!!")#查询用户信息def search(): global conn c = conn.cursor() i = input("请输入所要查询的联系人姓名或电话号码:") if i.isnumeric(): sql1 = "SELECT id,name,age, address, telenumber from TA where telenumber like '%" + i + "%'" cursor = c.execute(sql1) res=cursor.fetchall() if len(res)!=0: for row in res: print("id:{0}".format(row[0])) print("姓名:{0}".format(row[1])) print("年龄:{0}".format(row[2])) print("地址:{0}".format(row[3])) print("电话号码:{0}".format(row[4])) else: print("无此电话号码!!") else: sql1="SELECT id,name,age, address, telenumber from TA where name like '%"+i+"%'" cursor = c.execute(sql1) res=cursor.fetchall() if len(res) == 0: print("该联系人不存在!!") else: for row in res: print("id:{0}".format(row[0])) print("姓名:{0}".format(row[1])) print("年龄:{0}".format(row[2])) print("地址:{0}".format(row[3])) print("电话号码:{0}".format(row[4]))#显示所有用户信息def showall(): global conn c = conn.cursor() cursor = c.execute("SELECT id, name, age, address, telenumber from TA") for row in cursor: print("id:{0}".format(row[0])) print("姓名:{0}".format(row[1])) print("年龄:{0}".format(row[2])) print("地址:{0}".format(row[3])) print("电话号码:{0}".format(row[4]))print("指令如下:\n1.输入\"add\"为通讯录添加联系人信息\n2.输入\"delete\"删除通讯录里的指定联系人信息 \n3.输入\"searchall\"查询通讯录里的所有用户 \n4.输入\"search\"根据姓名或手机号码查找信息 ")while 1: temp = input("请输入指令:") if temp == "add": insert() print("添加成功!") temp1=input("是否继续操作通讯录?(y or n)") if temp1=="n": print("成功退出!!") break else: continue elif temp=="delete": delete() temp1 = input("是否继续操作通讯录?(y or n)") if temp1 == "n": print("成功退出!!") break else: continue elif temp=="searchall": showall() temp1 = input("是否想继续操作通讯录?(y or n)") if temp1 == "n": print("成功退出!!") break else: continue elif temp=="search": search() temp1 = input("您是否想继续操作通讯录?(y or n)") if temp1 == "n": print("成功退出!!") break else: continue else: print("请输入正确指令!!")conn.close()#关闭数据库
以上就是关于"Python怎么编写通讯录通过数据库存储实现模糊查询功能"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
输入
查询
信息
数据
号码
联系人
联系
数据库
通讯
通讯录
电话
电话号码
姓名
成功
存储
用户
内容
功能
地址
年龄
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
opc服务器和历史数据库
对数据库输出
应用服务器最低配置
开源 服务器管理软件
手机社交服务器
sql+数据库如何附加
5g网络安全设备招标中新赛克
云丁网络技术 官网
淄博设备软件开发咨询
网络安全开发包详解pdf
软件开发费用摊销
MacBook上的数据库软件
校园网络安全问题与对策
经典网络技术诈骗案例
泰拉瑞亚服务器推荐
如何加速访问微软服务器
机房巡检必须要网络安全员吗
我的世界服务器决战斗罗外挂
网站数据库导入导出
软件开发咨询流程
中央网络技术
做网络安全品牌的代理
数据库保留的字
网络安全防护设备设施有哪些
数据库添加外键
整体上传文件到服务器
我的世界服务器如何锁定队友位置
网络安全考试题库
云服务器连不上
数据库中 逻辑独立性