Python implements simplified address book modification and python address book modification
Description:
I wrote a simple address book in my previous blog, but I still think it is not perfect:
You need to enter the ID. Although the ID is the primary key, the auto-increment function is not implemented;
I forgot to add a phone number;
If the inserted usernames are the same, the subsequent query, modification, and deletion functions will be affected;
Therefore, the modified version mainly fixes the above three defects. The details are as follows:
Set the user ID to an auto-increment field, that is, you do not need to enter the ID. The program will automatically sort the inserted users and attach the ID, starting from 1 by default. SQLite implements the auto-Increment Function of the primary key, please refer to the SQLiteAUTOINCREMENT/auto increment blog.
The phone number field is added when the table is created;
The inserted username cannot be the same;
Okay, I don't want to talk about it anymore. Add the new Code. Haha ~~~
#-*-Coding: UTF-8-*-import sqlite3 # Open the local database to store user information. conn = sqlite3.connect ('mysql _ person. db') # create a table in the database. The code used to create a table needs to be commented out after the first execution. Otherwise, the following message will be displayed when the program is executed again: the TABLE already has conn.exe cute (''' 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"; # Add User Information def insert (): USER_NAME = raw_input ('Please Enter the user nickname: ') cursor = conn.exe cute ("SELECT name from MT_table1 where name =' % s';" % USER_NAME) for row in cursor: if USER_NAME = row [0]: print "sorry, the user name already exists. Enter the user name" break else: AGE = input ('Enter the AGE: ') again :') ADDRESS = raw_input ('Enter the user ADDRESS: ') SALARY = input ('Enter the user SALARY:') PhoneNumber = input ("Enter the contact information :") # To Avoid code being too long, use the character string connection feature to shorten the code sql1 = 'insert INTO MT_table1 (NAME, AGE, ADDRESS, SALARY, PhoneNumber) 'sql1 + = 'values ("% S", "% d", "% s", "% d", "% d"); '% (USER_NAME, AGE, ADDRESS, SALARY, PhoneNumber) conn.exe cute (sql1) conn. commit () print "Records insert successfully" # delete user information def delete (): delete_name = raw_input ("enter the name of the contact to be deleted :") cursor = conn.exe cute ("SELECT name from MT_table1 where name = '% s';" % delete_name) for row in cursor: if delete_name = row [0]: conn.exe cute ("DELETE from MT_table1 where name = '% s';" % delete_name) Conn. commit () print "Records delete successfully" break else: print "sorry, this user does not exist" # modify user information def modify (): update_name = raw_input ("enter the name of the user to be modified:") sql6 = "SELECT name from MT_table1 where name = '% s';" % update_name cursor = conn.exe cute (sql6) for row in cursor: if update_name = row [0]: New_addr = raw_input ("Enter the new address of the user to be modified :") new_age = input ("Enter the new age of the user to be modified:") New_salary = input ("Enter the new salary of the user to be modified:") New_num = Input ("Enter the new user number to be modified:") 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.exe cute (sql3) conn. commit () print "modified successfully" sql5 = "SELECT id, name, age, address, salary, PhoneNumber from MT_table1 where name = '% s '; "% update_name cursor = conn.exe cute (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, this user information does not exist" # query user information def search (): conn = sqlite3.connect ('mysql _ person. db ') search_name = raw_input ('Enter the name of the user to query') sql2 = "SELECT id, name, age, address, salary, phoneNumber from MT_table1 where name = '% s'; "% (search_name) cursor = conn.exe cut E (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, no such user information" # display all user information def showall (): cursor = conn.exe cute ("SELECT id, name, age, address, salary, PhoneNumber from MT_table1 ") for row in cursor: print" ID = ", row [0] print" NAME = ", row [1] prin T "AGE =", row [2] print "ADDRESS =", row [3] print "SALARY =", row [4] print "PhoneNumber = ", row [5], "\ n" print "Operation done successfully"; cursor = conn.exe cute ("select count (*) from MT_table1;") for row in cursor: print "A total of % d users" % row [0] def menu (): print '1. add the contact 'print' 2. delete the contact 'print' 3. modify the contact 'print' 4. query the contact 'print' 5. show all contacts 'print' 6. exit the program 'print' What do you want to do? 'While True: menu () x = raw_input ('enter your selection menu number:') 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 "Thank you! "Exit () continue else: print" the input option does not exist. Please enter it again! "Continue
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.