[英]Python - Insert 300 variables into SQLite
我每分鍾從熱傳感器接收 300 個值。 這 300 個值需要插入到 SQLite 數據庫中,因為它們每分鍾收到一次。
我在 SQLite 數據庫中創建了 302 行,第一列是S_ID
,第二列是timestamp
。 在這里,每次添加一行時S_ID
都會自動遞增,並且timestamp
列的默認值是當前系統時間。 我已經設定這樣的,我收到的每分鍾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)) +
')')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.