繁体   English   中英

根据Sqlite3中的上一个条目自动递增

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM