2014-05-23 4 views
0

웹 기반 POS 시스템을 만들고 있습니다.다중 sqlite3 인서트와 관련된 문제

@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 오류가 발생하고 그것이 왜 잘 모르겠어요 :이 플라스크 코드를 통해

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 
); 

: 사용자되면 "주문 제출"데이터가이 스키마를 통해 제출 클릭 사고. 두 개의 다른 db.execute 문을 수행해야합니까?

+1

나는 플라스크-SQLAlchemy의에서 살펴 본다 제안했다. 코드 내에 SQL 쿼리를 작성할 필요가 없습니다. – IanAuld

+0

감사합니다. – metersk

+1

http://pythonhosted.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application – IanAuld

답변

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'],)) 
관련 문제