Python 如何使用sqlite为一组行创建唯一ID
我有以下数据库模式:Python 如何使用sqlite为一组行创建唯一ID,python,sqlite,flask,Python,Sqlite,Flask,我有以下数据库模式: 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_
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
);
数据通过以下代码发送至数据库:
@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()
db.execute('insert into order_items (SKU, product_name, unit_price, quantity) values (?, ?, ?, ?)',
[d['sku'], d['name'], d['price'], d['quantity']])
db.execute('insert into orders (total_price) values (?)',
[d['subtotal']])
db.commit()
return jsonify(location=url_for('thankyou'))
读取订单表可得出以下结论:
1|1187.86
2|1187.86
3|1187.86
4|1187.86
5|1187.86
6|1187.86
7|102.92
8|102.92
9|102.92
10|102.92
并读取订单项目表,得出以下结果:
|ASD|Hot Sauce|10.99|1
|JKL|Chilli Peppers|8.99|1
|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
|MEE|Energy Drinks|10.99|1
|FUE|Literally, Anything|1|1
|POL|Burger Ryan|1000|1
|ASD|Hot Sauce|10.99|1
|JKL|Chilli Peppers|8.99|1
|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
|MEE|Energy Drinks|10.99|1
我试图完成的是生成一个唯一的事务ID,该ID将以特定顺序提供给每个项目
首先,我不确定为什么order\u items表中的transaction\u id列没有出现。第二,我怎样才能得到一张可能是这样的桌子:
订单:
1|1187.86
1|1187.86
1|1187.86
1|1187.86
1|1187.86
1|1187.86
2|102.92
2|102.92
2|102.92
2|102.92
订购物品:
1|ASD|Hot Sauce|10.99|1
1|JKL|Chilli Peppers|8.99|1
1|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
1|MEE|Energy Drinks|10.99|1
1|FUE|Literally, Anything|1|1
1|POL|Burger Ryan|1000|1
2|ASD|Hot Sauce|10.99|1
2|JKL|Chilli Peppers|8.99|1
2|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
2|MEE|Energy Drinks|10.99|1
这是我能做的事吗
编辑:
这就是我如何更改CL.的答案以符合我的代码:
@app.route('/load_ajax', methods=["GET", "POST"])
def load_ajax():
if request.method == "POST":
data = request.get_json()
db = get_db()
c = db.cursor()
c.execute('insert into orders (total_price) values (?)', [data[0]['subtotal']])
transaction_id = c.lastrowid
for group in groupby(data, itemgetter('name')):
id, data_list = group
for d in data_list:
db.execute('insert into order_items (transaction_id, SKU, product_name, unit_price, quantity) values (?, ?, ?, ?, ?)',
[transaction_id, d['sku'], d['name'], d['price'], d['quantity']])
db.commit()
return jsonify(location=url_for('thankyou'))
order\u items
表中的transaction\u id
列没有出现,因为您没有在其中插入任何内容
您不希望在orders
表中有重复的行。
只插入一次订单(我假设一个帖子就是一个订单):
您可以在订单和订单项目之间使用映射表
CREATE TABLE orders (
transaction_id integer primary key autoincrement,
);
CREATE TABLE order_mapping (
id integer primary key autoincrement,
transaction_id foreign key references orders(transaction_id),
item_id foreign key references order_items(item_id)
);
CREATE TABLE order_items (
item_id integer primary key autoincrement,
SKU integer not null,
product_name text not null,
unit_price integer not null,
quantity integer not null
);
谢谢,这正是我想要的。我不得不做一些修改以适应我的数据结构(在我的回答中公布)
CREATE TABLE orders (
transaction_id integer primary key autoincrement,
);
CREATE TABLE order_mapping (
id integer primary key autoincrement,
transaction_id foreign key references orders(transaction_id),
item_id foreign key references order_items(item_id)
);
CREATE TABLE order_items (
item_id integer primary key autoincrement,
SKU integer not null,
product_name text not null,
unit_price integer not null,
quantity integer not null
);