Python怎么编写通讯录通过数据库存储实现模糊查询功能
发表于:2024-09-24 作者:千家信息网编辑
千家信息网最后更新 2024年09月24日,这篇"Python怎么编写通讯录通过数据库存储实现模糊查询功能"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,
千家信息网最后更新 2024年09月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安全错误
数据库的锁怎样保障安全
计算机网络技术就业好吗
网络安全机房系统全套应急预案
常州私有服务器安装
国际版有没有刺激战场的服务器
服务器运行报告模板
神秘网络安全守护者
中国网络安全英文报道
牛总数据库
国企下属网络安全公司
安全组修改了需要重启云服务器吗
默纳克修改服务器协议
广发数据库
三级网络技术题库最新
浙江绍兴服务器防御ddos
软件开发培训武汉热干面
数据库实验操作
黑色玫瑰lol服务器
putty远程管理服务器
什么是软件开发工程师助理
株洲市网络安全和信息化委员会
好用的数据库分析
数据库行级锁讲解
网络安全法中明确网络运营者
网络安全学习哪些专业
备忘录会被软件开发人看到吗
河北现代软件开发厂家价格
淘客数据库
图片数据库如何建立
美旺网络技术服务
软件开发邀标函