
代碼如下:
# 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!!!")