首页 > 解决方案 > 将整数列表插入数据库列

问题描述

我正在尝试将整数列表插入数据表的列中。如果我运行以下代码:

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", list_with_integers)
conn.commit()

我收到以下错误:

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", list_with_integers)
ValueError: parameters are of unsupported type

我不明白我做错了什么。我已经检查并确保列表的所有元素都是整数,如果我尝试运行

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", [list_with_integers])

我得到:

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", [list_with_integers])
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 559 supplied.

问题:如何解决此问题并将整数列表插入一列?

标签: sqlpython-3.xsqlite

解决方案


executemany方法的参数应该是一个元组序列,因此您应该将整数列表转换为 1 元组序列:

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", map(lambda i: (i,), list_with_integers))

推荐阅读