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 "你輸入錯誤,請重新輸入"
新聞熱點
疑難解答
圖片精選