From 1d9f305cf944ad31a2b88d57132c5beeceae3033 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 26 Dec 2010 22:09:23 -0500 Subject: [PATCH] moved sqlite code to DataStore.py --- DataStore.py | 40 ++++++++++++++++++++++++++++++++++++++++ dwtt | 32 +------------------------------- 2 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 DataStore.py diff --git a/DataStore.py b/DataStore.py new file mode 100644 index 0000000..ba25caf --- /dev/null +++ b/DataStore.py @@ -0,0 +1,40 @@ + +import sqlite3 +import os + +class DataStore: + def __init__(self, timedbfile): + if not os.path.exists(timedbfile): + self.createdb(timedbfile) + self.conn = sqlite3.connect(dbfile) + + def __del__(self): + self.conn.close() + + def createdb(self, dbfile): + conn = sqlite3.connect(dbfile) + c = conn.cursor() + c.execute(''' +CREATE TABLE tasks ( + id INTEGER PRIMARY KEY, + name TEXT, + longname TEXT, + parent TEXT +)''') + c.execute(''' +CREATE TABLE entries ( + date INTEGER PRIMARY KEY, + taskid INTEGER, + hours REAL, + FOREIGN KEY (taskid) REFERENCES tasks(id) +)''') + c.execute(''' +CREATE TABLE history ( + id INTEGER PRIMARY KEY, + taskid INTEGER, + FOREIGN KEY (taskid) REFERENCES tasks(id) +)''') + conn.commit() + c.close() + conn.close() + diff --git a/dwtt b/dwtt index ad4a252..c6a63f0 100755 --- a/dwtt +++ b/dwtt @@ -2,8 +2,6 @@ from CmdWindow import CmdWindow from Command import Command -import sqlite3 -import os import sys import getopt from datetime import datetime @@ -39,35 +37,7 @@ def main(argv): sys.exit(2) timedbfile = args[0] - if not os.path.exists(timedbfile): - createdb(timedbfile) - -def createdb(dbfile): - conn = sqlite3.connect(dbfile) - c = conn.cursor() - c.execute(''' -CREATE TABLE tasks ( - id INTEGER PRIMARY KEY, - name TEXT, - longname TEXT, - parent TEXT -)''') - c.execute(''' -CREATE TABLE entries ( - date INTEGER PRIMARY KEY, - taskid INTEGER, - hours REAL, - FOREIGN KEY (taskid) REFERENCES tasks(id) -)''') - c.execute(''' -CREATE TABLE history ( - id INTEGER PRIMARY KEY, - taskid INTEGER, - FOREIGN KEY (taskid) REFERENCES tasks(id) -)''') - conn.commit() - c.close() - conn.close() + ds = DataStore(timedbfile) def doCmdWindow(): c = CmdWindow()