python操作數(shù)據(jù)庫之sqlite3打開數(shù)據(jù)庫、刪除、修改示例
#coding=utf-8
__auther__ = 'xianbao'
import sqlite3
# 打開數(shù)據(jù)庫
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
#查詢?nèi)康男畔?/P>
def showalldata():
print "-------------------處理后后的數(shù)據(jù)-------------------"
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("請輸入您的聯(lián)系電話:"))
return username1, passworld1, address1, telnum1
# (添加) 往數(shù)據(jù)庫中添加內(nèi)容
def adddata():
welcome = """-------------------歡迎使用添加數(shù)據(jù)功能---------------------"""
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 "-----------------恭喜你數(shù)據(jù),添加成功----------------"
showalldata()
hel[1].close()
# (刪除)刪除數(shù)據(jù)庫中的內(nèi)容
def deldata():
welcome = "------------------歡迎您使用刪除數(shù)據(jù)庫功能------------------"
print welcome
delchoice = raw_input("請輸入您想要刪除用戶的編號:")
hel = opendata() # 返回游標(biāo)conn
hel[1].execute("delete from tianjia where id ="+delchoice)
hel[1].commit()
print "-----------------恭喜你數(shù)據(jù),刪除成功----------------"
showalldata()
hel[1].close()
# (修改)修改數(shù)據(jù)的內(nèi)容
def alter():
welcome = "--------------------歡迎你使用修改數(shù)據(jù)庫功能-----------------"
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()
# 查詢數(shù)據(jù)
def searchdata():
welcome = "--------------------歡迎你使用查詢數(shù)據(jù)庫功能-----------------"
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 "-------------------恭喜你,你要查找的數(shù)據(jù)如下---------------------"
print ("您查詢的數(shù)據(jù)編號是%s" % id1)
print ("您查詢的數(shù)據(jù)名稱是%s" % username)
print ("您查詢的數(shù)據(jù)密碼是%s" % passworld)
print ("您查詢的數(shù)據(jù)地址是%s" % address)
print ("您查詢的數(shù)據(jù)電話是%s" % telnum)
cur.close()
hel[1].close()
# 是否繼續(xù)
def contnue1(a):
choice = raw_input("是否繼續(xù)?(y or n):")
if choice == 'y':
a = 1
else:
a = 0
return a
if __name__ == "__main__":
flag = 1
while flag:
welcome = "---------歡迎使用仙寶數(shù)據(jù)庫通訊錄---------"
print welcome
choiceshow = """
請選擇您的進一步選擇:
(添加)往數(shù)據(jù)庫里面添加內(nèi)容
(刪除)刪除數(shù)據(jù)庫中內(nèi)容
(修改)修改書庫的內(nèi)容
(查詢)查詢數(shù)據(jù)的內(nèi)容
選擇您想要的進行的操作:
"""
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 "你輸入錯誤,請重新輸入"
相關(guān)文章
Python關(guān)于sys.argv[]的用法及說明
sys.argv[]是Python中用于從程序外部獲取參數(shù)的列表,參數(shù)索引從0開始,0索引代表腳本名稱本身,后續(xù)索引代表傳遞給腳本的參數(shù),通過指定索引可以獲取特定的參數(shù),如sys.argv[1]獲取第一個傳入?yún)?shù),當(dāng)傳入多個參數(shù)時,可以通過切片或循環(huán)獲取全部參數(shù)2024-09-09使用Python?Turtle庫帶你玩轉(zhuǎn)創(chuàng)意繪圖(畫個心,寫個花)
Python的turtle庫提供了一種有趣且易于上手的編程繪圖方式,適合初學(xué)者學(xué)習(xí),通過本文的介紹,你將了解到如何進行畫布設(shè)置、畫筆屬性的調(diào)整、畫筆的移動與控制,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-11-11python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
這篇文章主要介紹了python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10用Pytorch訓(xùn)練CNN(數(shù)據(jù)集MNIST,使用GPU的方法)
今天小編就為大家分享一篇用Pytorch訓(xùn)練CNN(數(shù)據(jù)集MNIST,使用GPU的方法),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python標(biāo)準(zhǔn)庫中的logging用法示例詳解
logging是Python標(biāo)準(zhǔn)庫中記錄常用的記錄日志庫,通過logging模塊存儲各種格式的日志,主要用于輸出運行日志,可以設(shè)置輸出日志的等級、日志保存路徑、日志文件回滾等,這篇文章主要介紹了Python標(biāo)準(zhǔn)庫中的logging,需要的朋友可以參考下2022-09-09