02 sqlite數據庫應用(2)學生通訊錄

1、數據庫使用實例——學生通訊錄

import sqlite3

打開數據庫

def opendb():
    conn = sqlite3.connect('mydb.db')
    cur = conn.execute('''create table if not exists tongxunlu(usernum integer primary key, 
                                                               username varchar(128), 
                                                               password varchar(128),
                                                               address varchar(125), 
                                                               telnum varchar(128))''')
    return cur, conn

查詢全部信息

def showalldb():
    print('----------------處理後的數據--------------------')
    hel = opendb()
    cur = hel[1].cursor()
    cur.execute('select * from tongxunlu')
    res = cur.fetchall()
    for line in res:
        for h in line:
            print(h, end = ',')
        print()
    cur.close()

輸入信息

def into():
    usernum = input('請輸入學號:')
    username1 = input('請輸入姓名:')
    password1 = input('請輸入密碼:')
    address1 = input('請輸入地址:')
    telnum1 = input('請輸入聯繫電話:')
    return usernum, username1, password1, address1, telnum1

往數據庫中添加內容

def adddb():
    welcome = "-------------歡迎使用添加數據功能------------------"
    print(welcome)
    person = into()
    hel = opendb()
    hel[1].execute("""insert into tongxunlu(usernum, username, password, address, telnum) values(?,?,?,?,?)""",
                  (person[0], person[1], person[2], person[3], person[4]))
    hel[1].commit()
    print('------------恭喜你,數據添加成功---------------')
    showalldb()
    hel[1].close()

刪除數據庫中的內容

def deldb():
    welcome = "-------------歡迎使用刪除數據庫功能------------------"
    print(welcome)
    delchoice = input('請輸入想要刪除學號:')
    hel = opendb()
    hel[1].execute("""delete from tongxunlu where usernum = """ + delchoice)
    hel[1].commit()
    print('------------恭喜你,數據刪除成功---------------')
    showalldb()
    hel[1].close()

修改數據庫的內容

def alter():
    welcome = "-------------歡迎使用修改數據庫功能------------------"
    print(welcome)
    
    changechoice = input('請輸入想要修改的學生的學號:')
    hel = opendb()
    person = into()
    hel[1].execute("""update tongxunlu set usernum=?,username=?,password=?,address=?,telnum=? where usernum = """ + 
                   changechoice,(person[0], person[1], person[2], person[3], person[4]))
    hel[1].commit()
    print('------------恭喜你,數據修改成功---------------')
    showalldb()
    hel[1].close()

查詢數據

def searchdb():
    welcome = "-------------歡迎使用查詢數據庫功能------------------"
    print(welcome)
    choice = input('請輸入要查詢的學生的學號:')
    hel = opendb()
    cur = hel[1].cursor()
    cur.execute('select * from tongxunlu where usernum = ' + choice)
    hel[1].commit()
    print('------------恭喜你,你要查找的數據如下---------------')
    for row in cur:
        print(row[0], row[1], row[2], row[3], row[4])
    cur.close()
    hel[1].close()

是否繼續

def conti():
    choice = input('是否繼續?(y or n):')
    a = 1 if choice == 'y' else 0
    return a

主程序

if __name__ == '__main__':
    flag = 1
    while flag:
        welcome = '--------歡迎使用數據庫通訊錄---------'
        print(welcome)
        choiceshow = """
        請選擇您的進一步選擇:
        (添加)往數據庫裏面添加內容
        (刪除)刪除數據庫中的內容
        (修改)修改數據庫的內容
        (查詢)查詢數據庫的內容
        選擇您想要進行的操作:
        """
        choice = input(choiceshow)
        if choice == '添加':
            adddb()
            flag = conti()
        elif choice == '刪除':
            deldb()
            flag = conti()
        elif choice == '修改':
            alter()
            flag = conti()
        elif choice == '查詢':
            searchdb()
            flag = conti()
        else:
            print('你輸入錯誤,請重新輸入')

--------歡迎使用數據庫通訊錄---------

    請選擇您的進一步選擇:
    (添加)往數據庫裏面添加內容
    (刪除)刪除數據庫中的內容
    (修改)修改數據庫的內容
    (查詢)查詢數據庫的內容
    選擇您想要進行的操作:
    修改

-------------歡迎使用修改數據庫功能------------------
請輸入想要修改的學生的學號:1234
請輸入學號:1084
請輸入姓名:5463
請輸入密碼:5435
請輸入地址:4154
請輸入聯繫電話:54
------------恭喜你,數據修改成功---------------
----------------處理後的數據--------------------
1084,5463,5435,4154,54,
是否繼續?(y or n):n

改進

1、當輸入錯誤的時候,要提醒而不是報錯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章