0

I am creating a web-based POS system. Once the users clicks the "order submit" data is submitted via this 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
);

Through 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 (?, ?, ?, ?); insert into orders (total_price) values (?)',
                [d['sku'], d['name'], d['price'], d['quantity']],[d['subtotal']])
                db.commit()
        return jsonify(location=url_for('thankyou'))

I am getting a 500 error and I'm not sure why it is happening. Do I need to do two different db.execute statements?

2
  • 1
    I would suggest taking a look at Flask-SQLAlchemy. You won't have to write SQL queries like that inside your code. Commented May 23, 2014 at 18:00
  • 1
    pythonhosted.org/Flask-SQLAlchemy/… Commented May 23, 2014 at 18:05

1 Answer 1

2

Yes, you need to use two different .execute() statements. Quoting the cursor.execute() documentation:

execute() will only execute a single SQL statement.

So do so:

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'],))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.