2017-06-07 42 views
1

我試圖管理SQLite數據庫通過更新兩個行(只有浮動),它們在時間間隔中的所有平均值。平均值確定後,應刪除兩行中的一行。我正在查找的SQL語句被嵌入到Pythonscript中,該語句每天由cronjob調用一次。如何使未定義的許多SQL更新

例如:

中的cronjob

1|Node1|45.39|25.07|1013.3|2.086|2017-06-01 17:43:43 
2|Node1|48.07|25.07|1013.4|2.081|2017-06-01 17:43:57 
3|Node1|45.37|25.08|1013.3|2.08|2017-06-01 17:44:01 
4|Node1|48.04|25.07|1013.3|1.991|2017-06-01 17:44:15 
5|Node1|45.39|25.08|1013.3|1.991|2017-06-01 17:48:36 
6|Node1|45.47|25.03|1013.4|1.991|2017-06-01 17:50:57 
7|Node1|45.48|25.03|1013.3|1.991|2017-06-01 17:52:09 
8|Node1|45.46|25.04|1013.3|1.991|2017-06-01 17:53:26 
9|Node1|45.25|25.12|1013.3|2.092|2017-06-01 17:57:20 
10|Node1|45.43|25.02|1013.2|2.093|2017-06-01 18:03:19 

中的cronjob

1|Node1|46.73|25.07|1013.35|2.084|2017-06-01 17:43:43 
2|Node1|46.705|25.075|1013.3|2.081|2017-06-01 17:44:01 
3|Node1|45.43|25.055|1013.35|1.999|2017-06-01 17:48:36 
4|Node1|48.47|25.035|1013.3|1.991|2017-06-01 17:52:09 
5|Node1|45.35|25.07|1013.25|2.092|2017-06-01 17:57:20 

之後之前我真的不知道如何解決這個問題,這是最有效的方式。當然,可以對行數進行計數,並對每兩行進行循環。在我的情況下,這可能意味着cronjob會執行超過1000個sql-querys,這看起來效率不高。

編輯 繼Pythonclass創建的SQLite數據庫

conn = sqlite3.connect('/var/www/sqliteDatabases/database_sdr.db') #connect to database "database.db" 

cur = conn.cursor()        
    #create a cursor 


    #______create table for temperature and humidity with date and time______ 
cur.execute('''CREATE TABLE measurement(
         ID INTEGER PRIMARY KEY AUTOINCREMENT, 
         nodeId text, 
         humidity float, 
         temperature float, 
         pressure float, 
         voltage float, 
         datetime text)''') c 

conn.commit() conn.close() 

編輯2:

這是被調用一次,每天由一個cronjob的main.py。

from includes.MyDatabase import MyDatabase 
from includes.MyDates import MyDates 

uri = '/var/www/sqliteDatabases/database_sdr.db' 

dt = MyDates() 
db = MyDatabase(uri) 
db.sum_up_week(dt.weekago) 
db.sum_up_month(dt.monthago) 
db.sum_up_year(dt.yearago) 
db.close() 

這是更新和刪除數據庫(對於現在的sum_up功能沒有SQL語句)數據庫類:

import sqlite3 
import sys 

class MyDatabase(): 

    def __init__(self, uri): 
     self.uri = uri 
     try: 
      self.conn = sqlite3.connect(self.uri) #connect to database "wsn.db" 
      self.cur = self.conn.cursor()   #create a cursor 
     except sqlite3.Error as err: 
      print('Database connection failed with this path:'+self.uri+' Error: '), err.args[0] 
      exit() 

    def sum_up_week(self, week): 
     print(week)  #e.g. 2017-06-01 

    def sum_up_month(self, month): 
     print(month) #e.g. 2017-05-08 

    def sum_up_year(self, year): 
     print(year)  #e.g. 2016-06-08 

    def close(self): 
     self.conn.commit() #save the determined rows          
     self.conn.close() #close connection to DB 
+0

顯示錶格模式。第一列是否保證包含連續值? –

+0

第一列是否保證包含連續值? –

+0

當然!如您所見,第一列(ID)是自動增量的。 – N3wbie

回答

1

要獲得連續的ID值,所有內容複製到一個臨時表。然後使用自連接將行與相應的偶數和奇數ID值組合起來,並將所有內容複製回來:

CREATE TEMPORARY TABLE t1 (
    id INTEGER PRIMARY KEY, 
    nodeId, 
    humidity, 
    temperature, 
    pressure, 
    voltage, 
    datetime 
); 

INSERT INTO t1 
SELECT NULL, nodeId, humidity, temperature, pressure, voltage, datetime 
FROM measurement; 

CREATE TEMPORARY TABLE t2 AS 
SELECT a.nodeId       AS nodeId, 
     (a.humidity + b.humidity )/2 AS humidity, 
     (a.temperature + b.temperature)/2 AS temperature, 
     (a.pressure + b.pressure )/2 AS pressure, 
     (a.voltage  + b.voltage )/2 AS voltage, 
     a.datetime       AS datetime 
FROM t1 AS a 
JOIN t1 AS b ON a.id + 1 = b.id 
      AND (a.id % 2) == 1; 

DELETE FROM measurement; 

INSERT INTO measurement 
SELECT NULL, nodeId, humidity, temperature, pressure, voltage, datetime 
FROM t2; 

DROP TABLE t1; 
DROP TABLE t2; 
+0

好吧,這看起來像我正在尋找的最有效的方式。從未考慮過創建臨時表。我非常感謝這個有用的答案。我會嘗試這個並報告我的結果。最好的問候! – N3wbie