Python如何編寫通訊錄通過數(shù)據(jù)庫存儲(chǔ)實(shí)現(xiàn)模糊查詢功能-創(chuàng)新互聯(lián)

這篇文章主要介紹Python如何編寫通訊錄通過數(shù)據(jù)庫存儲(chǔ)實(shí)現(xiàn)模糊查詢功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的白銀網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

1.要求

數(shù)據(jù)庫存儲(chǔ)通訊錄,要求按姓名/電話號(hào)碼查詢,查詢條件只有一個(gè)輸入入口,自動(dòng)識(shí)別輸入的是姓名還是號(hào)碼,允許模糊查詢。

2.實(shí)現(xiàn)功能

可通過輸入指令進(jìn)行操作。

(1)首先輸入“add”,可以對(duì)通訊錄進(jìn)行添加聯(lián)系人信息。

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”,可以刪除指定的聯(lián)系人信息。

輸入姓名刪除:

cursor = c.execute( "SELECT name from TA where name = '%s';" %i)

輸入電話號(hào)碼刪除:

cursor = c.execute( "SELECT name from TA where telenumber= '%s';" % i)

(3)輸入“search”,可以輸入聯(lián)系人或者電話號(hào)碼,查詢聯(lián)系人信息,這里實(shí)現(xiàn)了模糊查詢以及精確查詢。

輸入姓名查詢:

sql1 = "SELECT id,name,age, address, telenumber from TA where telenumber like '%" + i +
"%'"
cursor = c.execute(sql1)

輸入電話號(hào)碼查詢:

sql1= "SELECT id,name,age, address, telenumber from TA where name like '%" +i+
"%'"
cursor = c.execute(sql1)

(4)輸入“searchall”,查詢?nèi)柯?lián)系人信息。

cursor = c.execute( "SELECT id, name, age, address, telenumber from TA" )

3.數(shù)據(jù)庫sqlite3

Python自帶一個(gè)輕量級(jí)的關(guān)系型數(shù)據(jù)庫sqlite。這一數(shù)據(jù)庫使用SQL語言。sqlite作為后端數(shù)據(jù)庫,可以搭配Python建網(wǎng)站,或者制作有數(shù)據(jù)存儲(chǔ)需求的工具。sqlLite還在其它領(lǐng)域有廣泛的應(yīng)用,比如HTML5和移動(dòng)端。Python標(biāo)準(zhǔn)庫中的sqlite3提供該數(shù)據(jù)庫的接口。因此此次使用了sqlite3數(shù)據(jù)庫存儲(chǔ)通訊錄的聯(lián)系人信息。

源碼:

import sqlite3
import re
#打開本地?cái)?shù)據(jù)庫用于存儲(chǔ)用戶信息
conn = sqlite3.connect('mysql_telephone_book.db')
c = conn.cursor()
#在該數(shù)據(jù)庫下創(chuàng)建表,創(chuàng)建表的這段代碼在第一次執(zhí)行后需要注釋掉,否則再次執(zhí)行程序會(huì)一直提示:該表已存在
'''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()#提交當(dāng)前的事務(wù)
#增加用戶信息
def insert():
  global conn
  c = conn.cursor()
  ID=int(input("請(qǐng)輸入id號(hào):"))
  name=input("請(qǐng)輸入姓名:")
  age=int(input("請(qǐng)輸入年齡:"))
  address=input("請(qǐng)輸入地址:")
  telenumber=input("請(qǐng)輸入電話號(hào)碼:")
  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("請(qǐng)輸入所要?jiǎng)h除的聯(lián)系人姓名或電話號(hào)碼:")
  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("成功刪除聯(lián)系人信息!!")
        break
    else:
      print("該聯(lián)系人不存在!!")
  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("成功刪除聯(lián)系人信息?。?quot;)
        break
    else:
      print("該電話號(hào)碼錯(cuò)誤??!")
#查詢用戶信息
def search():
  global conn
  c = conn.cursor()
  i = input("請(qǐng)輸入所要查詢的聯(lián)系人姓名或電話號(hào)碼:")
  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("電話號(hào)碼:{0}".format(row[4]))
    else:
      print("無此電話號(hào)碼!!")
  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("該聯(lián)系人不存在??!")
    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("電話號(hào)碼:{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("電話號(hào)碼:{0}".format(row[4]))
print("指令如下:\n1.輸入\"add\"為通訊錄添加聯(lián)系人信息\n2.輸入\"delete\"刪除通訊錄里的指定聯(lián)系人信息 \n3.輸入\"searchall\"查詢通訊錄里的所有用戶 \n4.輸入\"search\"根據(jù)姓名或手機(jī)號(hào)碼查找信息 ")
while 1:
  temp = input("請(qǐng)輸入指令:")
  if temp == "add":
    insert()
    print("添加成功!")
    temp1=input("是否繼續(xù)操作通訊錄?(y or n)")
    if temp1=="n":
      print("成功退出??!")
      break
    else:
      continue
  elif temp=="delete":
    delete()
    temp1 = input("是否繼續(xù)操作通訊錄?(y or n)")
    if temp1 == "n":
      print("成功退出??!")
      break
    else:
      continue
  elif temp=="searchall":
    showall()
    temp1 = input("是否想繼續(xù)操作通訊錄?(y or n)")
    if temp1 == "n":
      print("成功退出!!")
      break
    else:
      continue
  elif temp=="search":
    search()
    temp1 = input("您是否想繼續(xù)操作通訊錄?(y or n)")
    if temp1 == "n":
      print("成功退出??!")
      break
    else:
      continue
  else:
    print("請(qǐng)輸入正確指令?。?quot;)
conn.close()#關(guān)閉數(shù)據(jù)庫

以上是“Python如何編寫通訊錄通過數(shù)據(jù)庫存儲(chǔ)實(shí)現(xiàn)模糊查詢功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站欄目:Python如何編寫通訊錄通過數(shù)據(jù)庫存儲(chǔ)實(shí)現(xiàn)模糊查詢功能-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://muchs.cn/article46/ddhehg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計(jì)網(wǎng)站收錄、網(wǎng)站內(nèi)鏈ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)