国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python操作數據庫之sqlite3打開數據庫、刪除、修改示例

2019-11-25 18:29:07
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

#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 "你輸入錯誤,請重新輸入"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荆州市| 乐安县| 东乌珠穆沁旗| 蒙阴县| 柘城县| 大石桥市| 云霄县| 河间市| 惠来县| 琼中| 墨江| 瑞安市| 朝阳市| 浮山县| 金寨县| 松潘县| 桃源县| 进贤县| 察哈| 监利县| 越西县| 财经| 潮安县| 黄骅市| 姚安县| 达孜县| 长武县| 颍上县| 咸丰县| 皋兰县| 苍梧县| 兴山县| 都匀市| 抚顺县| 呼图壁县| 时尚| 和顺县| 济源市| 华池县| 石景山区| 阿鲁科尔沁旗|