今天看啥  ›  专栏  ›  Alyna_C

【基础】学习笔记36-Python3 SQLite数据库-实操2

Alyna_C  · 简书  ·  · 2020-12-13 08:42


代码如下:

# SQlite3应用:简单订单管理系统

import sqlite3

def getConnection():  # 连接数据库

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():  # 获得订单数据

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():  # 判断是否继续操作

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!!!")




原文地址:访问原文地址
快照地址: 访问文章快照