Python實現通訊錄(1)
阿新 • • 發佈:2019-02-09
說實話,第一次寫這麼長的Python程式碼,期間遇到了很多問題,但是,最終還是完成了,花了我一天半的時間,嘿嘿~~~
該程式實現了使用者的增,刪,改,查,主要用到sqlite3模組。對於該模組的知識點,請檢視我的另一篇文章SQLite-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