1. 程式人生 > >Python實現簡易通訊錄(修改版)

Python實現簡易通訊錄(修改版)

描述:

上一篇部落格我寫了一個簡單的通訊錄,但是還是覺得不夠完美:

  • 需要輸入ID,雖然ID是主鍵,但是沒有實現自增功能;

  • 忘記加電話號碼了;

  • 如果插入使用者名稱相同,則後續的查、改、刪功能受到影響;

所以,我這個修改版主要修正了以上三點缺陷。具體如下

  • 將使用者ID設定為自增欄位,即就是不用輸入ID,程式會自動幫你將插入的使用者進行排序並附上ID,預設從1開始;SQLite實現主鍵自增功能,請檢視SQLiteAUTOINCREMENT/自動遞增這篇部落格。
  • 建立表時增加了電話號碼欄位;
  • 插入的使用者名稱不能重複;

好了,廢話不多說,附上新程式碼,哈哈~~~

#-*- coding:utf-8 -*-
import sqlite3 #開啟本地資料庫用於儲存使用者資訊 conn = sqlite3.connect('mysql_person.db') #在該資料庫下建立表,建立表的這段程式碼在第一次執行後需要註釋掉,否則再次執行程式會一直提示:該表已存在 conn.execute ('''CREATE TABLE MT_table1( ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL, PhoneNumber INT NOT NULL);'''
) print "Table created successfully"; #增加使用者資訊 def insert(): USER_NAME = raw_input('請輸入使用者暱稱:') cursor = conn.execute("SELECT name from MT_table1 where name = '%s';"%USER_NAME) for row in cursor: if USER_NAME == row[0]: print "sorry,改使用者名稱已存在,請重新輸入使用者名稱" break
else: AGE = input('請輸入年齡:') ADDRESS = raw_input('請輸入使用者地址:') SALARY = input('請輸入使用者薪水:') PhoneNumber = input("請輸入聯絡方式:") #為避免程式碼太長,以下我用字串的連線特性將程式碼縮短 sql1 = 'INSERT INTO MT_table1(NAME,AGE,ADDRESS,SALARY,PhoneNumber)' sql1 += 'VALUES("%s","%d","%s","%d","%d");'%(USER_NAME,AGE,ADDRESS,SALARY,PhoneNumber) conn.execute(sql1) conn.commit() print "Records insert successfully" #刪除使用者資訊 def delete(): delete_name = raw_input("請輸入所要刪除的聯絡人姓名:") cursor = conn.execute("SELECT name from MT_table1 where name = '%s';"%delete_name) for row in cursor: if delete_name == row[0]: conn.execute("DELETE from MT_table1 where name = '%s';"%delete_name) conn.commit() print "Records delete successfully" break else: print "sorry,不存在該使用者" #修改使用者資訊 def modify(): update_name = raw_input("請輸入要修改使用者的姓名:") sql6 = "SELECT name from MT_table1 where name = '%s';"%update_name cursor = conn.execute(sql6) for row in cursor: if update_name == row[0]: New_addr = raw_input("請輸入要修改使用者的新地址:") New_age = input("請輸入要修改使用者的新年齡:") New_salary = input("請輸入要修改使用者的新薪水:") New_num = input("輸入要修改使用者的新號碼:") sql3 = "UPDATE MT_table1 set address = '%s',age = '%d',salary = '%d',PhoneNumber = '%d' where \ name = '%s';"%(New_addr,New_age,New_salary,New_num,update_name) conn.execute(sql3) conn.commit() print "修改成功" sql5 = "SELECT id, name, age,address, salary ,PhoneNumber from MT_table1 where name = '%s';"%update_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] print "PhoneNumber = ",row[5], "\n" break else: print "sorry,不存在該使用者資訊" #查詢使用者資訊 def search(): conn = sqlite3.connect('mysql_person.db') search_name = raw_input('請輸入要查詢的使用者姓名') sql2 = "SELECT id,name,age, address, salary ,PhoneNumber from MT_table1 where name= '%s';" % (search_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] print "PhoneNumber = ",row[5], "\n" break else: print "sorry,沒有該使用者資訊" #顯示所有使用者資訊 def showall(): cursor = conn.execute("SELECT id, name,age, address, salary ,PhoneNumber from MT_table1") for row in cursor: print "ID = ", row[0] print "NAME = ", row[1] print "AGE = ",row[2] print "ADDRESS = ", row[3] print "SALARY = ", row[4] print "PhoneNumber = ",row[5], "\n" print "Operation done successfully"; cursor = conn.execute("select count(*) from MT_table1;") 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