0

trying to use this query with escaped variables:

"UPDATE table_name SET status = ?, status_by = ? WHERE (unique_id, page_id) IN (?)", [req.body.action, req.body.status_by, aSet]

but end up getting this error: 'Operand should contain 2 column(s)'

this is how aSet looks: (1234567890, 123),(1234567890, 123)

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Carlos
  • 1

1 Answers1

1

You can't use a ? placeholder for an entire list, it can only be replaced with a single value.

You need to spread the all the elements of aSet into separate parameters, and put enough ? placeholders in the query for all of them.

qs = ",".join(["(?, ?)"]*len(aSet))
sql = """UPDATE table_name SET status = ?, status_by = ? 
    WHERE (unique_id, page_id) IN (%s)"""%(qs)
cur.execute(sql, [req.body.action, req.body.status_by] + list(sum(aSet, ()))

I got that list(sum(aSet, ()) expression from Transform "list of tuples" into a flat list or a matrix

Barmar
  • 741,623
  • 53
  • 500
  • 612