我正在创建一个基于网络的POS系统。用户点击“订单提交”后,数据将通过此架构提交:
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 (?, ?, ?, ?); insert into orders (total_price) values (?)',
[d['sku'], d['name'], d['price'], d['quantity']],[d['subtotal']])
db.commit()
return jsonify(location=url_for('thankyou'))
我收到500错误,我不确定为什么会这样。我是否需要执行两个不同的db.execute语句?
答案 0 :(得分:2)
是的,您需要使用两个不同的.execute()
语句。引用cursor.execute()
documentation:
execute()
只会执行一个SQL语句。
所以这样做:
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'],))