I am having a problem with SELECT operation during CRUD operations. I want to pull the query from the database and throw it into a table. Why is %s error here? Can you help me?
@app.route('/Index')
def querypage():
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
error = ''
if request.method == 'POST':
PRODUCT = request.form['PRODUCT']
PRICE = request.form['PRICE']
sql = "SELECT * FROM TBSHOPING WHERE PRODUCT = %s && PRICE = %s"
cur.execute(sql)
productlist = cur.fetchall()
return render_template('datatable.html', productlist=productlist)
Error:
return super().execute(query, vars)
psycopg2.errors.SyntaxError: syntax error at or near "%"
LINE 1: SELECT * FROM TBSHOPING WHERE PRODUCT = %s && PRICE = %s
Edit:
I am using a sql statement in the query. I want to throw the data that will come from the database into the table I created in datatable.html. But I am getting %s error in my query sentence.
-
Please, check How to Ask. Don't post images of code, error, etc. Copy/paste as formatted text here.buran– buran2021年10月19日 09:36:46 +00:00Commented Oct 19, 2021 at 9:36
-
Please try to explain the question more briefly copy your code and use it with an answer explain how you are using post etc.Aditya Dev– Aditya Dev2021年10月19日 09:43:18 +00:00Commented Oct 19, 2021 at 9:43
-
I fixed it, can you check it again?user17190275– user171902752021年10月19日 09:57:18 +00:00Commented Oct 19, 2021 at 9:57
1 Answer 1
pass values for the placeholders like
cur.execute(sql,(PRODUCT,PRICE))
answered Oct 19, 2021 at 17:34
Siddhartha Kakaraparthy
212 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Siddhartha Kakaraparthy
Hello, the error says that the if block may or may not execute but we are executing this statement cur.execute(sql, (PRODUCT, PRICE)) in which PRODUCT and PRICE are not assigned any value.So just assign some dummy value to those variable like PRODUCT ="NA" and PRICE =0 before if statement
lang-py