亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python編寫通訊錄通過數據庫存儲實現模糊查詢功能

系統 1980 0

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編寫通訊錄通過數據庫存儲實現模糊查詢功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品国产成人高清在线 | 成人亚洲精品一区 | 日本午夜色 | 青草成人| 99热久久国产综合精品久久国产 | 欧美色网 | 国产精品98视频全部国产 | 国产一区二区精品久久 | 天天想夜夜操 | 欧美专区在线播放 | 高清一级毛片一本到免费观看 | 亚洲精品香蕉一区二区 | 国产999视频| 奇米欧美成人综合影院 | 日韩成 | 在线观看视频中文字幕 | 中文字幕一区在线播放 | 九九精品热线免费观看6 | 三中文乱码视频 | 免费一级a毛片免费观看欧美大片 | 日韩精品一区二区三区在线观看l | 精品亚洲大全 | 国产欧美久久精品 | 欧美高清一区二区三区欧美 | 欧美成人免费大片888 | 日本三级11k影院在线 | 欧美一级毛片久久精品 | 欧美成人视| 在线精品国内视频秒播 | 亚洲最大视频网站 | 成人午夜毛片在线看 | 九九影院理论片私人影院 | 久久亚洲精品专区蓝色区 | 久久视热这只是精品222 | 久久久青草青青国产亚洲免观 | 欧美一级视频免费观看 | 欧美一级在线免费观看 | 欧美一级全部免费视频 | 婷婷在线网站 | 久久精品国产一区二区小说 | 国产精品社区在线观看 |