Python编写通讯录通过数据库存储实现模糊查询功能


Posted in Python onJuly 18, 2019

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 sqlite3
import 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编写通讯录通过数据库存储实现模糊查询功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
python批量下载图片的三种方法
Apr 22 Python
分享Python字符串关键点
Dec 13 Python
Python爬虫利用cookie实现模拟登陆实例详解
Jan 12 Python
Python中easy_install 和 pip 的安装及使用
Jun 05 Python
python中利用Future对象回调别的函数示例代码
Sep 07 Python
对python中的six.moves模块的下载函数urlretrieve详解
Dec 19 Python
python实现抖音点赞功能
Apr 07 Python
十个Python练手的实战项目,学会这些Python就基本没问题了(推荐)
Apr 26 Python
python随机生成大小写字母数字混合密码(仅20行代码)
Feb 01 Python
使用matplotlib动态刷新指定曲线实例
Apr 23 Python
python接入支付宝的实例操作
Jul 20 Python
Python基于百度AI实现抓取表情包
Jun 27 Python
Django 实现图片上传和显示过程详解
Jul 18 #Python
Django框架模型简单介绍与使用分析
Jul 18 #Python
python使用pandas处理excel文件转为csv文件的方法示例
Jul 18 #Python
django 2.2和mysql使用的常见问题
Jul 18 #Python
详解Python二维数组与三维数组切片的方法
Jul 18 #Python
Django框架视图介绍与使用详解
Jul 18 #Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
Jul 18 #Python
You might like
PHP实现货币换算的方法
2014/11/29 PHP
PHP mysqli事务操作常用方法分析
2017/07/22 PHP
Laravel 手动开关 Eloquent 修改器的操作方法
2019/12/30 PHP
网页设计常用的一些技巧
2006/12/22 Javascript
Javascript 日期处理之时区问题
2009/10/08 Javascript
input的focus方法使用
2010/03/13 Javascript
基于jquery的一个浮动框(扩展性比较好 )
2010/08/27 Javascript
javascript动态加载二
2012/08/22 Javascript
优化javascript的执行效率一些方法总结
2013/12/25 Javascript
使用CSS3的scale实现网页整体缩放
2014/03/18 Javascript
jquery图片倾斜层叠切换特效代码分享
2015/08/27 Javascript
javascript显示上周、上个月日期的处理方法
2016/02/03 Javascript
jQuery实现单击按钮遮罩弹出对话框效果(2)
2017/02/20 Javascript
基于Vue实现微信小程序的图文编辑器
2018/07/25 Javascript
JS实现换肤功能的方法实例详解
2019/01/30 Javascript
JS使用百度地图API自动获取地址和经纬度操作示例
2019/04/16 Javascript
vue实现二级导航栏效果
2019/10/19 Javascript
Node.js设置定时任务之node-schedule模块的使用详解
2020/04/28 Javascript
python使用正则搜索字符串或文件中的浮点数代码实例
2014/07/11 Python
跟老齐学Python之有点简约的元组
2014/09/24 Python
python修改操作系统时间的方法
2015/05/18 Python
python print 按逗号或空格分隔的方法
2018/05/02 Python
python方法生成txt标签文件的实例代码
2018/05/10 Python
详解【python】str与json类型转换
2019/04/29 Python
python rsync服务器之间文件夹同步脚本
2019/08/29 Python
pytorch的batch normalize使用详解
2020/01/15 Python
Django调用支付宝接口代码实例详解
2020/04/04 Python
在Python中使用K-Means聚类和PCA主成分分析进行图像压缩
2020/04/10 Python
详解css3中的伪类before和after常见用法
2020/11/17 HTML / CSS
美国运动鞋类和服装零售连锁店:Shoe Palace
2019/08/13 全球购物
自立自强的名人事例
2014/02/10 职场文书
大学生怎样写好自荐信
2014/02/25 职场文书
动员大会主持词
2014/03/20 职场文书
践行三严三实心得体会
2014/10/13 职场文书
python开发实时可视化仪表盘的示例
2021/05/07 Python
python unittest单元测试的步骤分析
2021/08/02 Python