简体   繁体   English

Python sqlite:使用未指定数量的参数的executemany()

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

I am using python3 with pyodbc library.我正在将 python3 与 pyodbc 库一起使用。 I am fetching data from the remote MSSQL database into the local sqlite file.我正在将远程 MSSQL 数据库中的数据提取到本地 sqlite 文件中。 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.效果很好,直到我有一个包含 50 多个字段的表。 Do i really have to write '?'我真的必须写'吗? fifty times?五十次?

Consider dynamically creating the prepared statement conditioned on length of prod_list :考虑根据prod_list的长度动态创建准备好的语句:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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