0

I am trying to run an sql function that inserts data into a table. 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)

1 Answer 1

1

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