我正在尝试运行一个将数据插入表中的sql函数。我按照here解释的示例,但每当我运行脚本时,没有数据插入到表中,脚本也不会返回消息。这是代码:我可能做错了什么?
from flask import render_template, flash, redirect, request
from app import app
from .forms import LoginForm
from .forms import RegistrationForm
import sqlite3 as sql
@app.route('/')
@app.route('/index')
@app.route('/registration', methods = ['GET','POST'])
def registration():
form = RegistrationForm()
if request.method == 'POST':
try:
card_id = request.form['card_id']
pin = request.form['pin']
account_id = request.form['account_id']
with sql.connect("testDB.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id) )
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("index.html", msg=msg)
con.close()
else:
return render_template("registration.html", form=form)
答案 0 :(得分:1)
我看到的一个问题,可能是拼写错误或其他问题,是您VALUES
的{{1}}子句中有四个占位符,但您似乎只是打算指定三个列。改变这个:
INSERT
到此:
cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id) )