user17190275
user17190275

Reputation:

Python database query

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.

Upvotes: 1

Views: 83

Answers (1)

pass values for the placeholders like

cur.execute(sql,(PRODUCT,PRICE))

Upvotes: 1

Related Questions