[英]Autoincrementing based on previous entry in Sqlite3
我正在創建一個基於Web的POS系統。 用戶單擊“訂單提交”后,將使用以下模式將每個項目發送到sqlite數據庫:
drop table if exists orders;
create table orders (
transaction_id integer primary key autoincrement,
total_price integer not null,
SKU integer not null,
product_name text not null,
unit_price integer not null,
quantity integer not null
);
通過此燒瓶代碼:
@app.route('/load_ajax', methods=["GET", "POST"])
def load_ajax():
if request.method == "POST":
data = request.get_json()
for group in groupby(data, itemgetter('name')):
id, data_list = group
for d in data_list:
print d['subtotal']
db = get_db()
order = db.execute('insert into orders (total_price, SKU, product_name, unit_price, quantity) values (?, ?, ?, ?, ?)',
[d['subtotal'], d['sku'], d['name'], d['price'], d['quantity']])
db.commit()
return jsonify(location=url_for('thankyou'))
最初創建架構時,我認為transaction_id integer primary key autoincrement
量足以滿足事務ID(該ID附加到訂單中的每個項目)的麻煩,但是有點忘記了訂單中可能有多個項目。 所以現在,每個項目都是它自己的主鍵,這不是我想要的。 一個訂單的sqlite3輸出如下所示:
1|61.45|ASD|Hot Sauce|10.99|1
2|61.45|JKL|Chilli Peppers|8.99|1
3|61.45|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
並且我希望第一列中的所有內容都為1。對我的架構可以做些什么以得到所需的操作嗎? 我不確定如何做到最好。
規范化您的數據庫。 將所有重復信息放入一個表中,並將針對每個項目更改的所有信息放入另一表中:
CREATE TABLE orders (
transaction_id integer primary key autoincrement,
total_price integer not null
);
CREATE TABLE order_items (
transaction_id integer REFERENCES orders(transaction_id),
SKU integer not null,
product_name text not null,
unit_price integer not null,
quantity integer not null
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.