您好,登錄后才能下訂單哦!
小編給大家分享一下Python如何實現通訊錄功能的案例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Python實現通訊錄功能的案例
#-*- coding:utf-8 -*- import sqlite3 #打開本地數據庫用于存儲用戶信息 conn = sqlite3.connect('mysql_person.db') #在該數據庫下創建表,創建表的這段代碼在第一次執行后需要注釋掉,否則再次執行程序會一直提示:該表已存在 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("請輸入所要刪除的聯系人姓名:") 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.新增聯系人' print '2.刪除聯系人' print '3.修改聯系人' print '4.查詢聯系人' print '5.顯示所有聯系人' 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
以上是“Python如何實現通訊錄功能的案例”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。