1

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.

3
  • Please, check How to Ask. Don't post images of code, error, etc. Copy/paste as formatted text here. Commented 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. Commented Oct 19, 2021 at 9:43
  • I fixed it, can you check it again? Commented Oct 19, 2021 at 9:57

1 Answer 1

1

pass values for the placeholders like

cur.execute(sql,(PRODUCT,PRICE))
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for comment. So what does this error mean? cur.execute(sql, (PRODUCT, PRICE)) # Execute the SQL UnboundLocalError: local variable 'PRODUCT' referenced before assignment
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.