您好,登錄后才能下訂單哦!
小編給大家分享一下Python如何實(shí)現(xiàn)通訊錄功能的案例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Python實(shí)現(xiàn)通訊錄功能的案例
#-*- coding:utf-8 -*- import sqlite3 #打開本地?cái)?shù)據(jù)庫用于存儲(chǔ)用戶信息 conn = sqlite3.connect('mysql_person.db') #在該數(shù)據(jù)庫下創(chuàng)建表,創(chuàng)建表的這段代碼在第一次執(zhí)行后需要注釋掉,否則再次執(zhí)行程序會(huì)一直提示:該表已存在 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('請(qǐng)輸入用戶ID:\n') NAME = raw_input('請(qǐng)輸入用戶昵稱:\n') AGE = input('請(qǐng)輸入年齡:\n') ADDRESS = raw_input('請(qǐng)輸入用戶地址:\n') SALARY = input('請(qǐng)輸入用戶薪水:\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("請(qǐng)輸入所要?jiǎng)h除的聯(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("請(qǐng)輸入要修改用戶的姓名:") print search() sql4 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name cursor = conn.execute(sql4) x = raw_input("請(qǐng)輸入要修改用戶的新地址:") y = input("請(qǐng)輸入要修改用戶的新年齡:") z = input("請(qǐng)輸入要修改用戶的新薪水:") 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('請(qǐng)輸入要查詢的用戶姓名') 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個(gè)用戶"%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('請(qǐng)輸入您的選擇菜單號(hào):') 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 "輸入的選項(xiàng)不存在,請(qǐng)重新輸入!" continue
以上是“Python如何實(shí)現(xiàn)通訊錄功能的案例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。