python - Python - 将 300 个变量插入 SQLite
问题描述
我每分钟从热传感器接收 300 个值。这 300 个值需要插入到 SQLite 数据库中,因为它们每分钟都会收到。
我在 SQLite 数据库中创建了 302 行,第一列是S_ID
,第二列是timestamp
. 在这里,S_ID
每次添加一行时都会自动递增,timestamp
column 的默认值是当前系统时间。我已经编程,每分钟接收 300 个热传感器值,将所有 300 个值放在一个名为的列表中data
并插入data
数据库。现在,我需要知道如何在executemany
不写下所有 300 个列名及?
以下的情况下编写语句。
data = [(300, 2, 4, ..., 5.5)] #these are 300 values that are inserted into a list when received from heat sensor
c.executemany('INSERT INTO heat_table (col3, col4, ..., col302) VALUES (?, ?, ..., ?)', data)
解决方案
我会用列表推导创建这些名称,然后加入它们:
query = ('INSERT INTO heat_table (' +
', '.join('col%d' % i for i in range(3, len(data) + 3)) +
') VALUES (' +
', '.join('?' * len(data)) +
')')
推荐阅读
- javascript - 获取带有 chrome 扩展名的选项卡图标并将其发送到 Flask [JS] [Python]
- java - (Java)如何创建一个循环,将我从数组生成的随机名称分配给多个字符串?
- ushort - 打开大文件并将其变成 ushort ?现有代码问题
- javascript - 使用 Dicer 将异步数据部分排序到文件中
- snowflake-cloud-data-platform - 找不到用户定义的函数
- google-apps-script - 如何以编程方式在 Google 文档中的特定位置添加表格?
- html - 如何使小 div 上的边框半径与大 div 相同
- powerbi - 计算 Power BI 中每个日期的不同 ID
- ckan - CKAN2.9 向数据集(包)表单添加上传字段
- python - expiringdict,在到期前打印