0

I am trying to add data in the existing database but getting error output, There is a minor mistake please help me.Thanks

Code ::

@app.route('/')
def new_student():
    return render_template('student.html')

@app.route('/addrec', methods=['POST', 'GET'])

def addrec():
    if request.method == 'POST':
        try:
            nm = request.form['nm']
            addr = request.form['add']
            city = request.form['city']
            pin = request.form['pin']

            with sql.connect("database.db") as con:
                cur = con.cursor()
                cur.execute('''INSERT INTO students (name,addr,city,pin)
                    VALUES(?, ?, ?, ?)''',(nm,addr,city,pin))

                con.commit()
                msg = "Record successfully added"
        except:

            con.rollback()
            msg = "error in insert operation"

        finally:
            return render_template("result.html", msg=msg)
            con.close()

if __name__ == '__main__':
    app.run(debug=True)

My result.html file is :

<!doctype html>
<html>
   <body>
      result of addition : {{ msg }}
      <h2><a href = "\">go back to home page</a></h2>
   </body>
</html>

The output should be:: "Record successfully added" but I am getting an "error in the insert operation".

1
  • 2
    Can you remove the try/except and show us the traceback you get when you try again? Commented Jan 13, 2019 at 14:02

3 Answers 3

1

My guess is that you have a typo in your code:

addr = request.form['add']

should be

addr = request.form['addr']

But your code needs some fixes regarding the connection:

con.commit() is called on normal exit of the with block, and con.rollback() is called if the with block is exited because of an exception (see the docs). You don't need those two explicit calls.

Furthermore, you placed the con.close() after the return statement, which means it will never be executed. Just swap the lines, or consider opening the connection at the start of the program.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Buddy ,there was minor error i have corrected as you have suggested ,
0

After the except:, put a raise to re-raise the last exception and post here the traceback.

Possible problems are:

  • You don't have permission on database.db file
  • You didn't send one of the form keys and accessing request.form["some key"] is raising KeyError exception.

Comments

0

Struggled with this for many hours. First, you need to create a python file and run it to create a database. After that run the main python file. It worked for me. No need to change "add" to "addr", because in student.html, name is given as "add" Also create a home.html file

<!DOCTYPE HTML>  
<HTML>  
<head>  
     <title>home</title>  
</head>  
<body>  
   <h2>Hi, welcome to the website</h2>  
   <a href="/enternew">Add New Record</a><br><be>  
   <a href ="/list">Show List</a><br><be>  
</body>  
</html>  

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.