Python實現(xiàn)通訊錄功能
說實話,第一次寫這么長的Python代碼,期間遇到了很多問題,但是,最終還是完成了,花了我一天半的時間。
該程序?qū)崿F(xiàn)了用戶的增,刪,改,查,主要用到sqlite3模塊。對于該模塊的知識點,請查看我的另一篇文章SQLite-Python
如有更好的建議,請私信,本人將不勝榮幸,讓我們一起來學(xué)習(xí)Python!
#-*- coding:utf-8 -*- import sqlite3 #打開本地數(shù)據(jù)庫用于存儲用戶信息 conn = sqlite3.connect('mysql_person.db') #在該數(shù)據(jù)庫下創(chuàng)建表,創(chuàng)建表的這段代碼在第一次執(zhí)行后需要注釋掉,否則再次執(zhí)行程序會一直提示:該表已存在 conn.execute('''CREATE TABLE MT (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL);''') print "Table created successfully"; conn.close() #增加用戶信息 def insert(): ID = input('請輸入用戶ID:\n') NAME = raw_input('請輸入用戶昵稱:\n') AGE = input('請輸入年齡:\n') ADDRESS = raw_input('請輸入用戶地址:\n') SALARY = input('請輸入用戶薪水:\n') sql1 = 'insert into MT(ID,NAME,AGE,ADDRESS,SALARY)' sql1 += 'values("%d","%s","%d","%s","%d");'%(ID,NAME,AGE,ADDRESS,SALARY) conn.execute(sql1) conn.commit() print "Records insert successfully" #刪除用戶信息 def delete(): name = raw_input("請輸入所要刪除的聯(lián)系人姓名:") cursor = conn.execute("SELECT name from MT where name = '%s';"%name) for row in cursor: if name == row[0]: conn.execute("DELETE from MT where name = '%s';"%name) conn.commit() print "Records delete successfully" break else: print "sorry,不存在該用戶" #修改用戶信息 def modify(): name = raw_input("請輸入要修改用戶的姓名:") print search() sql4 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name cursor = conn.execute(sql4) x = raw_input("請輸入要修改用戶的新地址:") y = input("請輸入要修改用戶的新年齡:") z = input("請輸入要修改用戶的新薪水:") sql3 = "UPDATE MT set address = '%s',age = '%d',\ salary = '%d' where name = '%s';"%(x,y,z,name) conn.execute(sql3) conn.commit() print "修改成功" sql5 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name cursor = conn.execute(sql5) for row in cursor: print "ID = ", row[0] print "NAME = ", row[1] print "AGE = ",row[2] print "ADDRESS = ", row[3] print "SALARY = ", row[4], "\n" #查詢用戶信息 conn = sqlite3.connect('mysql_person.db') def search(): conn = sqlite3.connect('mysql_person.db') name = raw_input('請輸入要查詢的用戶姓名') sql2 = "SELECT id,name,age, address, salary from MT where name= '%s';" % (name) cursor = conn.execute(sql2) for row in cursor: print "ID = ", row[0] print "NAME = ", row[1] print "AGE = ",row[2] print "ADDRESS = ", row[3] print "SALARY = ", row[4], "\n" break else: print "sorry,沒有該用戶信息" #顯示所有用戶信息 def showall(): cursor = conn.execute("SELECT id, age, name, address, salary from MT") for row in cursor: print "ID = ", row[0] print "NAME = ", row[1] print "AGE = ",row[2] print "ADDRESS = ", row[3] print "SALARY = ", row[4], "\n" print "Operation done successfully"; cursor = conn.execute("select count(*) from MT;") for row in cursor: print "一共有%d個用戶"%row[0] def menu(): print '1.新增聯(lián)系人' print '2.刪除聯(lián)系人' print '3.修改聯(lián)系人' print '4.查詢聯(lián)系人' print '5.顯示所有聯(lián)系人' print '6.退出程序' print 'What do you want to do?' while True: menu() x = raw_input('請輸入您的選擇菜單號:') if x == '1': insert() continue if x == '2': delete() continue if x == '3': modify() continue if x == '4': search() continue if x == '5': showall() continue if x == '6': print "謝謝使用!" exit() continue else: print "輸入的選項不存在,請重新輸入!" continue
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Python函數(shù)中輸入任意數(shù)量參數(shù)的實例
今天小編就為大家分享一篇在Python函數(shù)中輸入任意數(shù)量參數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python Collections強大的數(shù)據(jù)結(jié)構(gòu)工具使用實例探索
這篇文章主要介紹了Python Collections強大的數(shù)據(jù)結(jié)構(gòu)工具的使用實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python代碼中引用已經(jīng)寫好的模塊、方法的兩種方式
這篇文章主要介紹了Python代碼中引用已經(jīng)寫好的模塊、方法,下面就介紹兩種方式,可以簡潔明了地調(diào)用自己在其他模塊寫的代碼,需要的朋友可以參考下2022-07-07解決python中遇到字典里key值為None的情況,取不出來的問題
今天小編就為大家分享一篇解決python中遇到字典里key值為None的情況,取不出來的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python實現(xiàn)識別手寫數(shù)字 Python圖片讀入與處理
這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)識別手寫數(shù)字,Python圖片的讀入與處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程)
這篇文章主要介紹了PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04