0

我编写了一个 python 函数,用于使用传感器的测量值更新两个 SQLite.db 文件表。我每 15 分钟在我的 Raspberry Pi 上存储一次数据。

由于 SD 卡 (16GB) 的硬盘空间有限,我需要删除数据动态。很明显,我需要一种算法来删除第一天(4 行/小时)、第一周(1 行/小时)、第一个月(1 行/2 小时)和过去一个月(1 行/4 小时)之后的数据。最后,所有超过一年的数据都应该被删除。

下面是更新数据的函数:

def database(save_to_db):        #save_to_db is a list with humidity[0] OR temperature[0] and integers of year[1], month[2], day[3], time[4]
conn = sqlite3.connect('database.db')                    #connect to database "database.db"
cur  = conn.cursor()                                     #create a cursor
#______Which measure is determined? ('%' = humidity, 'C' = temperature)_____
if '%' in save_to_db[0]:
#______Insert a row with index, humidity, date, time
    cur.execute(('''INSERT INTO hum VALUES (
                 NULL, ?,?,?,?,?)''')
                (save_to_db[0], save_to_db[1], save_to_db[2], save_to_db[3], save_to_db[4]))
elif 'C' in save_to_db[0]:
#______Insert a row with index, temperature, date, time
    cur.execute(('''INSERT INTO tem VALUES (
                 NULL, ?,?,?,?,?)'''),
                (save_to_db[0], save_to_db[1], save_to_db[2], save_to_db[3], save_to_db[4]))   
conn.commit()                                           #save the determined rows                                     
conn.close()                                            #close connection to DB

现在我想查询新的一天是否已经开始。如果这是真的,应该调用一个新函数来删除数据。它不是真正动态的,但我认为每天删除一次数据就足够了。我不知道我该怎么做。

我是 python 和 SQLite 的新手。

4

0 回答 0