複製代碼 代碼如下:
#coding=utf-8
__auther__ = 'xianbao'
import sqlite3
# 開啟資料庫
def opendata():
conn = sqlite3.connect("mydb.db")
cur = conn.execute("""create table if not exists tianjia(
id integer primary key autoincrement, username varchar(128), passworld varchar(128),
address varchar(125), telnum varchar(128))""")
return cur, conn
#查詢全部的資訊
def showalldata():
print "-------------------處理後後的資料-------------------"
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("請輸入您的使用者名稱:"))
passworld1 = str(raw_input("請輸入您的密碼:"))
address1 = str(raw_input("請輸入您的地址:"))
telnum1 = str(raw_input("請輸入您的聯絡電話:"))
return username1, passworld1, address1, telnum1
# (添加) 往資料庫中新增內容
def adddata():
welcome = """-------------------歡迎使用添加資料功能---------------------"""
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 "-----------------恭喜你資料,添加成功----------------"
showalldata()
hel[1].close()
# (刪除)刪除資料庫中的內容
def deldata():
welcome = "------------------歡迎您使用刪除資料庫功能------------------"
print welcome
delchoice = raw_input("請輸入您想要刪除使用者的編號:")
hel = opendata() # 返回遊標conn
hel[1].execute("delete from tianjia where id ="+delchoice)
hel[1].commit()
print "-----------------恭喜你資料,刪除成功----------------"
showalldata()
hel[1].close()
# (修改)修改資料的內容
def alter():
welcome = "--------------------歡迎你使用修改資料庫功能-----------------"
print welcome
changechoice = raw_input("請輸入你想要修改的使用者的編號:")
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()
# 查詢資料
def searchdata():
welcome = "--------------------歡迎你使用查詢資料庫功能-----------------"
print welcome
choice = str(raw_input("請輸入你要查詢的使用者的編號:"))
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 "-------------------恭喜你,你要尋找的資料如下---------------------"
print ("您查詢的資料編號是%s" % id1)
print ("您查詢的資料名稱是%s" % username)
print ("您查詢的資料密碼是%s" % passworld)
print ("您查詢的資料地址是%s" % address)
print ("您查詢的數據通訊是%s" % telnum)
cur.close()
hel[1].close()
# 是否繼續
def contnue1(a):
choice = raw_input("是否繼續?(y or n):")
if choice == 'y':
a = 1
else:
a = 0
return a
if __name__ == "__main__":
flag = 1
while flag:
welcome = "---------歡迎使用仙寶資料庫通訊錄---------"
print welcome
choiceshow = """
請選擇您的進一步選擇:
(添加)往資料庫裡面新增內容
(刪除)刪除資料庫中內容
(修改)修改書庫的內容
(查詢)查詢資料的內容
選擇您想要的進行的操作:
"""
choice = raw_input(choiceshow)
if choice == "添加":
adddata()
contnue1(flag)
elif choice == "刪除":
deldata()
contnue1(flag)
elif choice == "修改":
alter()
contnue1(flag)
elif choice == "查詢":
searchdata()
contnue1(flag)
else:
print "你輸入錯誤,請重新輸入"