0

I'm trying to enter a value from an HTML form into a postgresql database, and I'm getting a 500 error. It works fine if I manually enter the values into the program like this...

@app.route('/add', methods=['POST'])
def add():
 name = request.form['name']
 employer_id = random.random()%100000;
 g.conn.execute("INSERT INTO employers(employer_id,name) VALUES ('C005329438','Twitter')")
 return redirect('/')

But I get an error every time I send it the value from the form like this

@app.route('/add', methods=['POST'])
def add():
 name = request.form['name']
 employer_id = random.random()%100000;
 g.conn.execute("INSERT INTO employers(employer_id,name) VALUES (%d,%s)", employer_id, name)
 return redirect('/')
asked Apr 10, 2016 at 0:18
0

1 Answer 1

3
g.conn.execute("INSERT INTO employers(employer_id,name) VALUES (%d,%s)", employer_id, name)

should probably be:

from sqlalchemy import text
g.conn.execute(text("INSERT INTO employers(employer_id,name) VALUES (:id, :name)"),
 {"id": str(employer_id), "name": name})

text() provides backend neutral support for bind parameters.

Your employer id attribute seems to be a text type too according to your successful query, so I added the str() wrapping.

answered Apr 10, 2016 at 0:21
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.