您好,登錄后才能下訂單哦!
描述:
上一篇博客我寫了一個簡單的通訊錄,但是還是覺得不夠完美:
需要輸入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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。