Python編寫通訊錄通過(guò)數(shù)據(jù)庫(kù)存儲(chǔ)實(shí)現(xiàn)模糊查詢功能
1.要求
數(shù)據(jù)庫(kù)存儲(chǔ)通訊錄,要求按姓名/電話號(hào)碼查詢,查詢條件只有一個(gè)輸入入口,自動(dòng)識(shí)別輸入的是姓名還是號(hào)碼,允許模糊查詢。
2.實(shí)現(xiàn)功能
可通過(guò)輸入指令進(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() # 提交,否則無(wú)法保存
(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ù)庫(kù)sqlite3
Python自帶一個(gè)輕量級(jí)的關(guān)系型數(shù)據(jù)庫(kù)sqlite。這一數(shù)據(jù)庫(kù)使用SQL語(yǔ)言。sqlite作為后端數(shù)據(jù)庫(kù),可以搭配Python建網(wǎng)站,或者制作有數(shù)據(jù)存儲(chǔ)需求的工具。sqlLite還在其它領(lǐng)域有廣泛的應(yīng)用,比如HTML5和移動(dòng)端。Python標(biāo)準(zhǔn)庫(kù)中的sqlite3提供該數(shù)據(jù)庫(kù)的接口。因此此次使用了sqlite3數(shù)據(jù)庫(kù)存儲(chǔ)通訊錄的聯(lián)系人信息。
源碼:
import sqlite3
import re
#打開(kāi)本地?cái)?shù)據(jù)庫(kù)用于存儲(chǔ)用戶信息
conn = sqlite3.connect('mysql_telephone_book.db')
c = conn.cursor()
#在該數(shù)據(jù)庫(kù)下創(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()#提交,否則無(wú)法保存
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)系人信息!!")
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("無(wú)此電話號(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)輸入正確指令?。?)
conn.close()#關(guān)閉數(shù)據(jù)庫(kù)
總結(jié)
以上所述是小編給大家介紹的Python編寫通訊錄通過(guò)數(shù)據(jù)庫(kù)存儲(chǔ)實(shí)現(xiàn)模糊查詢功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Python使用sql語(yǔ)句對(duì)mysql數(shù)據(jù)庫(kù)多條件模糊查詢的思路詳解
- Python Django2 model 查詢介紹(條件、范圍、模糊查詢)
- python中數(shù)據(jù)庫(kù)like模糊查詢方式
- python Django中models進(jìn)行模糊查詢的示例
- Python操作mongodb數(shù)據(jù)庫(kù)進(jìn)行模糊查詢操作示例
- Python模糊查詢本地文件夾去除文件后綴的實(shí)例(7行代碼)
- mysql 模糊查詢 concat()的用法詳解
- mysql如何讓左模糊查詢也能走索引
- Python中使用MySQL模糊查詢的詳細(xì)方法
相關(guān)文章
Python GUI之tkinter窗口視窗教程大集合(推薦)
這篇文章主要介紹了Python GUI之tkinter窗口視窗教程大集合,看這一篇教程足了,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
PyTorch使用torch.nn.Module模塊自定義模型結(jié)構(gòu)方式
這篇文章主要介紹了PyTorch使用torch.nn.Module模塊自定義模型結(jié)構(gòu)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python人工智能語(yǔ)音合成實(shí)現(xiàn)案例詳解
這篇文章主要為大家介紹了Python人工智能語(yǔ)音合成實(shí)現(xiàn)案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
PyQt5+QtChart實(shí)現(xiàn)繪制區(qū)域圖
QChart是一個(gè)QGraphicScene中可以顯示的QGraphicsWidget。本文將利用QtChart實(shí)現(xiàn)區(qū)域圖的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
Python實(shí)現(xiàn)字典的key和values的交換
本文給大家分別介紹了在python3.0和2.7版本下實(shí)現(xiàn)字典的key和values的交換的程序代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-08-08
python numpy中multiply與*及matul 的區(qū)別說(shuō)明
這篇文章主要介紹了python numpy中multiply與*及matul 的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05

