[英]How to create a unique ID for a grouping of rows with sqlite
I have the below db schema: 我有以下数据库模式:
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
);
Data is sent to the db via this flask code: 数据通过以下烧瓶代码发送到数据库:
@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'))
A read of the orders table gives this: 读取订单表可以得到以下信息:
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
and a read of the order_items table give this: 并读取order_items表可得出以下信息:
|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
What I am trying to accomplish is generating a unique transaction ID that would be given to each item in a particular order. 我要完成的工作是生成一个唯一的交易ID,该ID将按特定顺序分配给每个项目。
First I'm not sure why transaction_id column in the order_items table is not appearing. 首先,我不确定为什么order_items表中的transaction_id列没有出现。 Second, how can I get a table that might look like this: 其次,如何获得一个看起来像这样的表:
orders: 命令:
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
order_items: ORDER_ITEMS:
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
Is this something I can do sqlite? 这是我可以做的事情吗?
EDIT: This is how I changed CL.'s answer to fit my code: 编辑:这就是我更改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'))
The transaction_id
column in the order_items
table is not appearing because you are not inserting anything into it. 由于未在其中插入任何内容,因此order_items
表中的transaction_id
列未出现。
You do not want to have duplicate rows in the orders
table. 您不希望orders
表中有重复的行。 Insert an order only once (I am assuming a single POST is a single order): 仅插入一次订单(我假设单个POST是单个订单):
data = request.get_json()
subtotal = data......
c = db.cursor()
c.execute('insert into orders (total_price) values (?)', [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()
You could use a mapping table between orders and order_item 您可以在orders和order_item之间使用映射表
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
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.