原創: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 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()#關閉數據庫

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM