简体   繁体   中英

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

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM