0

I have a query that inserts two values in PV_ID column.
The query is INSERT INTO Projects (PV_ID) VALUES ('166'),('222')

I need a python code that uses string formatting to pass those values.

Thing that I have tried already:

  1. l = ['166', '222']
    query = 'INSERT INTO Projects (PV_ID) VALUES (%s)'
    conn.execute(query, l)
    error message : The SQL contains 1 parameter markers, but 2 parameters were supplied',
  2. I have tried to unpack the list above and pass it as a tuples of values but the insertion failed.
    error message : There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement,
  3. I tried this too Insert list into my database using Python but failed.
    error message : The SQL contains 1 parameter markers, but 2 parameters were supplied',

Any help is appreciated.

  • You don't use the same query in any of the attempts. Have you tried using `INSERT INTO Projects (PV_ID) VALUES (%s),(%s)` ? – Panagiotis Kanavos Nov 24 '22 at 14:03
  • Yes. The error thrown is ```The SQL contains 0 parameter markers, but 2 parameters were supplied```. I just wanna know how to pass those values dynamically? –  Nov 24 '22 at 14:09
  • Many DB-API packages do not support generating multiple `VALUES` clauses in `cursor.execute`. Some support it via `cursor.executemany`, but you need to check the docs for the connector package that you are using (many will just generate multiple `INSERT` statements with a single `VALUES` clause). The parameter format for `executemany` would be a list of tuples, like `[(123,), (456,)]`. – snakecharmerb Nov 24 '22 at 16:40

0 Answers0