summaryrefslogtreecommitdiff
path: root/sql.py
blob: b3db16560d47809a83cde45062928f73df358d1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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