diff --git a/README.md b/README.md index 092553a..7be8fde 100644 --- a/README.md +++ b/README.md @@ -13,35 +13,6 @@ web based HBCI banking software ## how to poll your bank account for updates have a look at the hbciMail/config_mail.py example and the MailWebsiteChanges tool (https://github.com/mtill/MailWebsiteChanges) -# database model -~~~~ -CREATE TABLE transactions( - account TEXT NOT NULL, - timestamp INTEGER NOT NULL, - amount REAL NOT NULL, - currency TEXT NOT NULL, - name TEXT NOT NULL, - description TEXT NOT NULL, - category TEXT NULL, - note TEXT NULL, - id INTEGER PRIMARY KEY AUTOINCREMENT); - -CREATE INDEX account ON transactions(account); -CREATE INDEX timestamp ON transactions(timestamp ASC); - - -CREATE TABLE categories( - pattern TEXT NOT NULL, - field TEXT NOT NULL, - category TEXT NOT NULL -); - -CREATE INDEX category ON categories(category ASC); -~~~~ - -# database backup -~~~~ +## how to backup the transaction database sqlite3 my_database.sqlite ".backup backup_file.sqlite" OR: sqlite3 my_database .dump > my_database.back -~~~~ - diff --git a/dbmodel.sql b/dbmodel.sql new file mode 100644 index 0000000..4ed9732 --- /dev/null +++ b/dbmodel.sql @@ -0,0 +1,22 @@ +CREATE TABLE transactions( + account TEXT NOT NULL, + timestamp INTEGER NOT NULL, + amount REAL NOT NULL, + currency TEXT NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + category TEXT NULL, + note TEXT NULL, + id INTEGER PRIMARY KEY AUTOINCREMENT); + +CREATE INDEX account ON transactions(account); +CREATE INDEX timestamp ON transactions(timestamp ASC); + + +CREATE TABLE categories( + pattern TEXT NOT NULL, + field TEXT NOT NULL, + category TEXT NOT NULL +); + +CREATE INDEX category ON categories(category ASC); diff --git a/kontomodel.py b/kontomodel.py index db06b00..95412b9 100644 --- a/kontomodel.py +++ b/kontomodel.py @@ -12,10 +12,17 @@ class KontoModel: def __init__(self, sqlitefile): + isExistingDB = os.path.exists(sqlitefile) + self.conn = sqlite3.connect(sqlitefile) self.conn.row_factory = sqlite3.Row self.cursor = self.conn.cursor() + if not isExistingDB: + with open('dbmodel.sql', 'r') as thefile: + self.conn.executescript(thefile.read()) + print("initiated new database") + def close(self): self.conn.close()