38 lines
1 KiB
Python
38 lines
1 KiB
Python
import sqlite3
|
|
|
|
from settings import DB_NAME
|
|
|
|
DATE_FORMAT = "%Y-%m-%d %H:%M:%S%z"
|
|
|
|
class Database:
|
|
def __init__(self, path=DB_NAME):
|
|
self.conn = sqlite3.connect(path)
|
|
self.cur = self.conn.cursor()
|
|
self.setup()
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def setup(self):
|
|
self.execute('CREATE TABLE IF NOT EXISTS "tracking" (`number` VARCHAR(128) PRIMARY KEY, `service` VARCHAR(128));')
|
|
self.execute('CREATE TABLE IF NOT EXISTS "status" (`trackingnumber` VARCHAR(128), `timestamp` TIMESTAMP, `status` TEXT, FOREIGN KEY (`trackingnumber`) REFERENCES `tracking` (`number`) ON DELETE CASCADE);')
|
|
self.commit()
|
|
|
|
def execute(self, *args, **kwargs):
|
|
self.cur.execute(*args, **kwargs)
|
|
|
|
def fetchone(self):
|
|
return self.cur.fetchone()
|
|
|
|
def commit(self):
|
|
self.conn.commit()
|
|
|
|
def close(self):
|
|
self.cur.close()
|
|
self.conn.close()
|
|
|
|
def __exit__(self, *args, **kwargs):
|
|
pass
|
|
|
|
def __del__(self):
|
|
self.close() |