Home > OS >  Python sqlite: using executemany() with unspecified number of parameters
Python sqlite: using executemany() with unspecified number of parameters

Time:12-23

I am using python3 with pyodbc library. I am fetching data from the remote MSSQL database into the local sqlite file. I have a table named 'Products' in both databases, i'm doing something like this:

prod_list = remote_db.cursor.execute('select * from Products').fetchall()
local_db.cursor.executemany('INSERT INTO Products VALUES(?, ?, ?, ?)', prod_list)

Works well until i have a table with over 50 fields. Do i really have to write '?' fifty times?

CodePudding user response:

Consider dynamically creating the prepared statement conditioned on length of prod_list:

qmarks = ", ".join('?' for i in prod_list[0])

sql = f"INSERT INTO Products VALUES ({qmarks})"
local_db.cursor.executemany(sql, prod_list)
  • Related