add DataStore class to create sqlite3 db
This commit is contained in:
parent
8d70696c3b
commit
83258c5d18
41
DataStore.py
Normal file
41
DataStore.py
Normal file
@ -0,0 +1,41 @@
|
||||
|
||||
import sqlite3
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
class DataStore:
|
||||
def __init__(self, dbfile):
|
||||
if not os.path.exists(dbfile):
|
||||
self.createdb(dbfile)
|
||||
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 projects (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT
|
||||
)''')
|
||||
c.execute('''
|
||||
CREATE TABLE tasks (
|
||||
id INTEGER PRIMARY KEY,
|
||||
projectid INTEGER,
|
||||
name TEXT,
|
||||
UNIQUE (projectid, name),
|
||||
FOREIGN KEY (projectid) REFERENCES projects(id)
|
||||
)''')
|
||||
c.execute('''
|
||||
CREATE TABLE hours (
|
||||
date TEXT,
|
||||
taskid INTEGER,
|
||||
seconds INTEGER,
|
||||
PRIMARY KEY (date, taskid),
|
||||
FOREIGN KEY (taskid) REFERENCES tasks(id)
|
||||
)''')
|
||||
conn.commit()
|
||||
c.close()
|
||||
conn.close()
|
Loading…
x
Reference in New Issue
Block a user