小編給大家分享一下Python如何實現(xiàn)通訊錄功能的案例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站建設(shè)、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元太原做網(wǎng)站,已為上家服務(wù),為太原各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575Python實現(xiàn)通訊錄功能的案例
#-*- 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
以上是“Python如何實現(xiàn)通訊錄功能的案例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!