跟踪数据库中项目的最新版本

时间:2018-07-21 20:53:44

标签: python

我有一个python代码,可以处理一组sqlite数据库。它一一读取所有数据库,并检查每一行差异。如果当前项目与上次检查的数据库项目不同,则会在历史数据库中插入新行,并将版本列设置为正确的版本。

所有人都在工作。

但是,如果该条目是最新条目,现在我也想将状态设置为1。如果这样的话,该项目会有一个较新的条目,则较旧的状态应设置为0,而新的状态应设置为1,依此类推。

有什么想法可以实现吗? -实际上,我想知道哪个版本是最新版本。

谢谢。

安德烈亚斯

代码:

import glob
import re
import sqlite3
import json

dbs = glob.glob("SQLiteParser_*.db3")

history_db = sqlite3.connect("history.db3")
cursor_hist = history_db.cursor()
cursor_hist.execute("DROP TABLE IF EXISTS items_history")
cursor_hist.execute('CREATE TABLE "items_history" ("version"  TEXT,"_id"  INTEGER,"name"  TEXT,"description"  TEXT,"type"  INTEGER,"icon"  INTEGER,"ql"  INTEGER,"data"  BLOB,"status"  INTEGER)')
cursor_hist.execute("DROP TABLE IF EXISTS nanos_history")
cursor_hist.execute('CREATE TABLE "nanos_history" ("version"  TEXT,"_id"  INTEGER,"name"  TEXT,"description"  TEXT,"type"  INTEGER,"icon"  INTEGER,"ql"  INTEGER,"data"  BLOB,"status"  INTEGER)')

dif_result = {}
print(len(dbs))
for i in range(len(dbs)):
    if i+1 > len(dbs)-1:
        break
    matches = re.search("SQLiteParser_(.*?)\.db3", dbs[i])
    version = matches.group(1)
    matches = re.search("SQLiteParser_(.*?)\.db3", dbs[i+1])
    version_new = matches.group(1)    
    connection_frist = sqlite3.connect(dbs[i])
    connection_last = sqlite3.connect(dbs[i+1])    
    cursor_first = connection_frist.cursor()
    cursor_last = connection_last.cursor()    
    cursor_first.execute("SELECT * FROM items")
    result_frist = cursor_first.fetchall()  
    col_name_list = [tuple[0] for tuple in cursor_first.description]    
    print(col_name_list)
    data_first = []
    for r in result_frist:
        cursor_last.execute("SELECT * FROM items where _id = "+str(r[0]))
        result_last = cursor_last.fetchall()
        for r_last in result_last:
            dif = False
            for j in range(len(r)):
                if r[j] != r_last[j]:
                    dif = True
                    out = list(r_last)
                    out.insert(0,version+"_to_"+version_new)
                    cursor_hist.execute('''INSERT INTO items_history (version, _id, name, description, type, icon, ql, data, status) VALUES (?,?,?,?,?,?,?,?,?)''',out)                    
                    break  

history_db.commit()

0 个答案:

没有答案