简体   繁体   English

尝试将数据插入sqlite3表时出错

[英]error when trying to insert data to sqlite3 table

I am trying to run an sql function that inserts data into a table. 我正在尝试运行将数据插入表中的sql函数。 I am following the example explained here but whenever i run the script no data is inserted into the table and neither does the script return the message. 我按照此处说明的示例进行操作但是无论何时运行脚本,都不会将任何数据插入表中,并且脚本也不会返回消息。 Here is the code: What can i be possibly be doing wrong? 这是代码:我可能做错了什么?

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)

One problem I see, possibly a typo or something else, is that you have four placeholders in the VALUES clause of your INSERT , but you seem to only intend to specify three columns. 我看到的一个问题(可能是拼写错误或其他问题)是INSERTVALUES子句中有四个占位符,但您似乎只打算指定列。 Change this: 更改此:

cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id) )

to this: 对此:

cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?)",(card_id,pin,account_id) )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 得到 RuntimeError: unsupported value 当尝试使用 python 将二进制数据插入到 sqlite3 表中的 blob 字段时 - Got RuntimeError: unsupported value when trying to insert binary data into a blob field in a sqlite3 table using python 尝试将数据插入sqlite3时出错-错误绑定参数0-可能不受支持的类型 - Errors when trying to insert data into sqlite3 - Error binding parameter 0 - probably unsupported type Python SQLite3 插入返回无错误但表中无数据 - Python SQLite3 insert return no error but no data in the table 得到错误“sqlite3.InterfaceError:错误绑定参数 0 - 可能不受支持的类型。” 使用sqlite3 python将数据插入表时 - Got error 'sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.' when insert data into table using sqlite3 python Python - 'sqlite3.InterfaceError:错误绑定参数 0 - 可能是不支持的类型。' 使用 sqlite3 python 将数据插入表时 - Python - 'sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.' when insert data into table using sqlite3 python python将数据插入sqlite3表 - python insert data into sqlite3 table (sqlite3.OperationalError:没有这样的列:)尝试将值插入sqlite3 db时出错 - (sqlite3.OperationalError: no such column: ) error when trying to insert values into sqlite3 db 尝试解包 sqlite3 行时出错 - Error when trying to depack sqlite3 row sqlite3“插入”错误 - sqlite3 'insert into' error SQLITE3插入错误 - SQLITE3 Insert Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM