diff --git a/src/db.py b/src/db.py new file mode 100644 --- /dev/null +++ b/src/db.py @@ -0,0 +1,38 @@ +import sqlite3 +import time + +COMMIT_TIME=2 # s + +class DB: + def __init__(self,filename): + self.filename=filename + self.connection=sqlite3.connect(self.filename) + self.connection.row_factory=sqlite3.Row + self.commitTime=time.time() + + def setup(self): + # !! err if exists + self.connection.execute("""create table `hashes` ( + `sector_id` integer primary key, + `sector_hash` blob not null, + `updated` integer + )""") + + self.connection.commit() + + ## Inserts hash into the DB. + # + # @param sectorHash bytes object containing the hash + def insertHash(self,sectorId,sectorHash): + # !! if not connection + res=self.connection.execute("""insert or replace into `hashes` (`sector_id`, `sector_hash`, `updated`) + values (?, ?, strftime('%s','now'))""", (sectorId,sectorHash)) + t=time.time() + if t-self.commitTime > COMMIT_TIME: + self.connection.commit() + self.commitTime=t + return res + + def listHashes(self): + # !! if not connection + return self.connection.execute("""select * from `hashes` order by `sector_id` asc""")