sql - 将整数列表插入数据库列
问题描述
我正在尝试将整数列表插入数据表的列中。如果我运行以下代码:
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.
问题:如何解决此问题并将整数列表插入一列?
解决方案
该executemany
方法的参数应该是一个元组序列,因此您应该将整数列表转换为 1 元组序列:
c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", map(lambda i: (i,), list_with_integers))
推荐阅读
- android - 如何使用 Material Design 主题创建圆形按钮、更改其颜色并为其设置动画?
- sqlite - 如何使用 vscode 将 csv 文件导入 sqlite?
- java - 使用 ByteArray 请求 MultiPart/Form-Data
- node.js - Nodejs Mongoose - 检查数组对象值是否存在,如果不存在则将新对象添加到数组中
- android - 如何在android studio中读取输出张量(tflite)
- r - 如何使用 R 中的某些公式将季度数据分解为月度数据?
- python - 根据值对列表进行排序,如果相同的值使用索引对其进行排序
- c++ - volatile 和 mutex 是否确保 C++ 中的内存排序?
- java - “选择新构造函数”查询返回 java.lang.Object 而不是构造函数类的实例
- ruby - 奥丁计划:关于棋盘颜色的问题