【基礎】學習筆記36-Python3 SQLite數(shù)據(jù)庫-實操2


代碼如下:

# SQlite3應用:簡單訂單管理系統(tǒng)

import sqlite3

def getConnection():? # 連接數(shù)據(jù)庫

? ? dbstr = 'C:\\sqlite3\\test.db'

? ? con = sqlite3.connect(dbstr)

? ? cur = con.cursor()

? ? sqlstr = ("create table if not exists orderl(order_id integer primary key,order_dec varchar(20),price float,order_num integer,address varchar(30))")

? ? cur.execute(sqlstr)

? ? return con

def showAllData():? # 顯示所有內容

? ? print("------------display record------------")

? ? dbinfo = getConnection()

? ? cur = dbinfo.cursor()

? ? cur.execute("select * from orderl")

? ? for item in cur.fetchall():

? ? ? ? print(item)

? ? cur.close()

? ? dbinfo.close()

def getOderListInfo():? # 獲得訂單數(shù)據(jù)

? ? orderId = int(input("Please enter Order ID:"))

? ? orderDec = input("Please enter Oder description:")

? ? price = eval(input("Please enter price:"))

? ? Ordernum = eval(input("Please enter number:"))

? ? address = input("Please enter address:")

? ? return orderId, orderDec, price, Ordernum, address

def addRec():? # 添加記錄

? ? print("------------add record------------")

? ? record = getOderListInfo()

? ? dbinfo = getConnection()

? ? cur = dbinfo.cursor()

? ? print(record[0], record[1], record[2], record[3], record[4])

? ? cur.execute("insert into orderl values(?,?,?,?,?)",

? ? ? ? ? ? ? ? (record[0], record[1], record[2], record[3], record[4]))

? ? dbinfo.commit()

? ? print("------------add record success------------")

? ? showAllData()

? ? cur.close()

? ? dbinfo.close()

def delRec():? # 刪除記錄

? ? print("------------delete record------------")

? ? dbinfo = getConnection()

? ? cur = dbinfo.cursor()

? ? choice = input("Please enter deleted order_id:")

? ? cur.execute("delete from orderl where order_id="+choice)

? ? dbinfo.commit()

? ? print("------------delete record success------------")

? ? showAllData()

? ? cur.close()

? ? dbinfo.close()

def modifyRec():? # 修改記錄

? ? print("------------change record------------")

? ? dbinfo = getConnection()

? ? cur = dbinfo.cursor()

? ? choice = input("Please enter change order_id:")

? ? record = getOderListInfo()

? ? cur.execute("update orderl set order_id=?,order_dec=?,price=?,order_num=?,address=? where order_id=" +

? ? ? ? ? ? ? ? choice, (record[0], record[1], record[2], record[3], record[4]))

? ? dbinfo.commit()

? ? showAllData()

? ? print("------------change record success------------")

? ? cur.close()

? ? dbinfo.close()

def searchRec():? # 查找記錄

? ? print("------------search record------------")

? ? dbinfo = getConnection()

? ? cur = dbinfo.cursor()

? ? choice = input("Please enter change order_id:")

? ? cur.execute("select * from orderl where order_id=" + choice)

? ? dbinfo.commit()

? ? print("------------search record success------------")

? ? for row in cur:

? ? ? ? print(row[0], row[1],? row[2],? row[3],? row[4])

? ? cur.close()

? ? dbinfo.close()

def continueIf():? # 判斷是否繼續(xù)操作

? ? choice = input("continue(y/n)?")

? ? if str.lower(choice) == 'y':

? ? ? ? flag = 1

? ? else:

? ? ? ? flag = 0

? ? return flag

if __name__ == "__main__":

? ? getConnection()

? ? flag = 1

? ? while flag:

? ? ? ? print("------------OrderItem Manage System------------")

? ? ? ? menu = '''

? ? ? ? 1. Append Record

? ? ? ? 2. Delete Record

? ? ? ? 3. Change Record

? ? ? ? 4. Search Record

? ? ? ? Please enter order number:

? ? ? ? '''

? ? ? ? choice = input(menu)

? ? ? ? if choice == '1':

? ? ? ? ? ? addRec()

? ? ? ? ? ? flag = continueIf()

? ? ? ? elif choice == '2':

? ? ? ? ? ? delRec()

? ? ? ? ? ? flag = continueIf()

? ? ? ? elif choice == '3':

? ? ? ? ? ? modifyRec()

? ? ? ? ? ? flag = continueIf()

? ? ? ? elif choice == '4':

? ? ? ? ? ? searchRec()

? ? ? ? ? ? flag = continueIf()

? ? ? ? else:

? ? ? ? ? ? print("order number error!!!")

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容