diff options
Diffstat (limited to 'sql.py')
| -rw-r--r-- | sql.py | 121 |
1 files changed, 67 insertions, 54 deletions
@@ -1,6 +1,8 @@ import sqlite3 + class db: + # https://docs.python.org/3/library/sqlite3.html def __init__(self, dbfile): # final variable self.dbfile = dbfile @@ -8,64 +10,75 @@ class db: open(dbfile) except FileNotFoundError: self.setup() - def exec(cmd): + + def exec(cmd, param=None): + """ + A function to execute a database command without having to + create the cursor + """ con = sqlite3.connect(self.dbfile) cur = con.cursor - cur.execute(cmd) + if param == None: + cur.execute(cmd) + else: + cur.execute(cmd, param) + con.commit() + con.close() + + def setup(self): + """ + 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(self.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 (date, time), + FOREIGN KEY (date) REFERENCES temperature(date), + FOREIGN KEY (time) REFERENCES temperature(time) + ); + """ + ) con.commit() con.close() + pass -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 |
