diff options
Diffstat (limited to 'sql.py')
| -rw-r--r-- | sql.py | 71 |
1 files changed, 71 insertions, 0 deletions
@@ -0,0 +1,71 @@ +import sqlite3 + +class db: + def __init__(self, dbfile): + # final variable + self.dbfile = dbfile + try: + open(dbfile) + except FileNotFoundError: + self.setup() + def exec(cmd): + con = sqlite3.connect(self.dbfile) + cur = con.cursor + cur.execute(cmd) + con.commit() + con.close() + +def setup(dbfile): + ''' + Necessary when the database does not exist and the tables must be + created for the first time, otherwise not needed to be run. + ''' + con = sqlite3.connect(dbfile) + cur = con.cursor() + cur.execute( + ''' + CREATE TABLE temperature ( + date text not null, + time integer not null, + temperature real, + target real, + PRIMARY KEY (date, time) + ); + ''' + ) + cur.execute( + ''' + CREATE TABLE weather ( + date text not null, + temperature real, + wind real, + PRIMARY KEY (date), + FOREIGN KEY (date) REFERENCES temperature(date) + ); + ''' + ) + cur.execute( + ''' + CREATE TABLE schedule ( + day text not null, + time integer not null, + temperature real, + PRIMARY KEY (day, time) + ''' + ); + cur.execute( + ''' + CREATE TABLE history ( + date text not null, + time integer not null, + heating_on integer, + PRIMARY KEY (day, time), + FOREIGN KEY (date) REFERENCES temperature(date), + FOREIGN KEY (time) REFERENCES temperature(time) + ''' + ); + con.commit() + con.close() + pass + def export_csv(self): + return 0 |
