python操作數(shù)據(jù)庫(kù)之sqlite3打開(kāi)數(shù)據(jù)庫(kù)、刪除、修改示例
#coding=utf-8
__auther__ = 'xianbao'
import sqlite3
# 打開(kāi)數(shù)據(jù)庫(kù)
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("請(qǐng)輸入您的用戶名:"))
passworld1 = str(raw_input("請(qǐng)輸入您的密碼:"))
address1 = str(raw_input("請(qǐng)輸入您的地址:"))
telnum1 = str(raw_input("請(qǐng)輸入您的聯(lián)系電話:"))
return username1, passworld1, address1, telnum1
# (添加) 往數(shù)據(jù)庫(kù)中添加內(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ù)庫(kù)中的內(nèi)容
def deldata():
welcome = "------------------歡迎您使用刪除數(shù)據(jù)庫(kù)功能------------------"
print welcome
delchoice = raw_input("請(qǐng)輸入您想要?jiǎng)h除用戶的編號(hào):")
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ù)庫(kù)功能-----------------"
print welcome
changechoice = raw_input("請(qǐng)輸入你想要修改的用戶的編號(hào):")
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ù)庫(kù)功能-----------------"
print welcome
choice = str(raw_input("請(qǐng)輸入你要查詢的用戶的編號(hào):"))
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ù)編號(hào)是%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ù)庫(kù)通訊錄---------"
print welcome
choiceshow = """
請(qǐng)選擇您的進(jìn)一步選擇:
(添加)往數(shù)據(jù)庫(kù)里面添加內(nèi)容
(刪除)刪除數(shù)據(jù)庫(kù)中內(nèi)容
(修改)修改書庫(kù)的內(nèi)容
(查詢)查詢數(shù)據(jù)的內(nèi)容
選擇您想要的進(jìn)行的操作:
"""
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 "你輸入錯(cuò)誤,請(qǐng)重新輸入"
相關(guān)文章
Python關(guān)于sys.argv[]的用法及說(shuō)明
sys.argv[]是Python中用于從程序外部獲取參數(shù)的列表,參數(shù)索引從0開(kāi)始,0索引代表腳本名稱本身,后續(xù)索引代表傳遞給腳本的參數(shù),通過(guò)指定索引可以獲取特定的參數(shù),如sys.argv[1]獲取第一個(gè)傳入?yún)?shù),當(dāng)傳入多個(gè)參數(shù)時(shí),可以通過(guò)切片或循環(huán)獲取全部參數(shù)2024-09-09使用Python?Turtle庫(kù)帶你玩轉(zhuǎn)創(chuàng)意繪圖(畫個(gè)心,寫個(gè)花)
Python的turtle庫(kù)提供了一種有趣且易于上手的編程繪圖方式,適合初學(xué)者學(xué)習(xí),通過(guò)本文的介紹,你將了解到如何進(jìn)行畫布設(shè)置、畫筆屬性的調(diào)整、畫筆的移動(dòng)與控制,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11python驗(yàn)證碼識(shí)別教程之利用滴水算法分割圖片
這篇文章主要給大家介紹了關(guān)于python驗(yàn)證碼識(shí)別教程之利用滴水算法分割圖片的相關(guān)資料,文章中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06python3將變量寫入SQL語(yǔ)句的實(shí)現(xiàn)方式
這篇文章主要介紹了python3將變量寫入SQL語(yǔ)句的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
這篇文章主要介紹了python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Python基于pillow庫(kù)實(shí)現(xiàn)生成圖片水印
這篇文章主要介紹了Python基于pillow庫(kù)實(shí)現(xiàn)生成圖片水印,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09用Pytorch訓(xùn)練CNN(數(shù)據(jù)集MNIST,使用GPU的方法)
今天小編就為大家分享一篇用Pytorch訓(xùn)練CNN(數(shù)據(jù)集MNIST,使用GPU的方法),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python標(biāo)準(zhǔn)庫(kù)中的logging用法示例詳解
logging是Python標(biāo)準(zhǔn)庫(kù)中記錄常用的記錄日志庫(kù),通過(guò)logging模塊存儲(chǔ)各種格式的日志,主要用于輸出運(yùn)行日志,可以設(shè)置輸出日志的等級(jí)、日志保存路徑、日志文件回滾等,這篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)中的logging,需要的朋友可以參考下2022-09-09