Copy CodeThe code is as follows:
#coding =utf-8
__auther__ = ' Xianbao '
Import Sqlite3
# Open Database
Def opendata ():
conn = Sqlite3.connect ("mydb.db")
cur = Conn.execute ("" "CREATE table if not exists Tianjia (
ID Integer PRIMARY key autoincrement, username varchar (+), Passworld varchar (128),
Address varchar, Telnum varchar (128)) "" "
Return CUR, conn
#查询全部的信息
Def showalldata ():
Print "-------------------post-processing data-------------------"
Hel = OpenData ()
cur = hel[1].cursor ()
Cur.execute ("SELECT * from Tianjia")
res = Cur.fetchall ()
For line in Res:
For h in line:
Print H,
Print
Cur.close ()
#输入信息
def into ():
username1 = str (raw_input ("Please enter your username:"))
Passworld1 = str (raw_input ("Please enter your password:"))
Address1 = str (raw_input ("Please enter your address:"))
TELNUM1 = str (raw_input ("Please enter your contact number:"))
Return username1, Passworld1, Address1, TELNUM1
# (add) Add content to the database
Def adddata ():
Welcome = ""-------------------Welcome to the Add Data feature---------------------""
Print Welcome
person = into ()
Hel = OpenData ()
Hel[1].execute ("INSERT into Tianjia (username, Passworld, address, Telnum) VALUES (?,?,?,?)",
(Person[0], person[1], person[2], person[3]))
Hel[1].commit ()
Print "-----------------Congratulations on your data, add success----------------"
ShowAllData ()
Hel[1].close ()
# (delete) Delete the contents of the database
Def deldata ():
Welcome = "------------------Welcome to use the Delete database feature------------------"
Print Welcome
Delchoice = raw_input ("Please enter the number of the user you want to delete:")
Hel = OpenData () # returns the cursor conn
Hel[1].execute ("Delete from Tianjia where id =" +delchoice)
Hel[1].commit ()
Print "-----------------Congratulations on your data, delete success----------------"
ShowAllData ()
Hel[1].close ()
# (Modify) Modify the contents of the data
Def Alter ():
Welcome = "--------------------Welcome to using the Modify Database feature-----------------"
Print Welcome
Changechoice = raw_input ("Please enter the number of the user you want to modify:")
Hel =opendata ()
person = into ()
Hel[1].execute ("Update Tianjia Set username=?, passworld=?, address=?,telnum=? where id= "+changechoice,
(Person[0], person[1], person[2], person[3]))
Hel[1].commit ()
ShowAllData ()
Hel[1].close ()
# query data
Def searchdata ():
Welcome = "--------------------welcome you to use the query database feature-----------------"
Print Welcome
Choice = str (raw_input ("Please enter the number of the user you want to query:"))
Hel = OpenData ()
cur = hel[1].cursor ()
Cur.execute ("select * from Tianjia where id=" +choice)
Hel[1].commit ()
row = Cur.fetchone ()
ID1 = str (row[0])
Username = str (row[1])
Passworld = str (row[2])
Address = str (row[3])
Telnum = str (row[4])
Print "-------------------Congratulations, the data you are looking for are as follows---------------------"
Print ("The data number you queried is%s"% id1)
Print ("The data name you queried is%s"% username)
Print ("The data password you queried is%s"% Passworld)
Print ("The data address you queried is%s"% address)
Print ("The data phone you are querying is%s"% telnum)
Cur.close ()
Hel[1].close ()
# whether to continue
Def contnue1 (a):
Choice = Raw_input ("Do you want to continue? (Y or N): ")
if choice = = ' Y ':
A = 1
Else
A = 0
Return a
if __name__ = = "__main__":
Flag = 1
While flag:
Welcome = "---------Welcome to use the Fairy Treasure Database address Book---------"
Print Welcome
Choiceshow = "" "
Please select your further choice:
(add) Add content to the database
(delete) Delete the contents of the database
(modify) Modify the contents of the library
Querying the contents of a data (query)
Select the action you want to make:
"""
Choice = Raw_input (choiceshow)
if choice = = "Add":
AddData ()
CONTNUE1 (flag)
elif Choice = = "Delete":
Deldata ()
CONTNUE1 (flag)
elif Choice = = "Modify":
Alter ()
CONTNUE1 (flag)
elif Choice = = "Query":
Searchdata ()
CONTNUE1 (flag)
Else
Print "You entered the error, please re-enter"