簡體   English   中英

Python - 將 300 個變量插入 SQLite

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM