-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.py
111 lines (90 loc) · 3.19 KB
/
sqlite.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import sqlite3, time
from robin_stocks import stocks
from sqlite3 import Error
def execute(conn, query, params=None):
"""
Execute an SQLite Query
"""
res = None
with conn:
try:
if params is None:
res = conn.cursor().execute(query).fetchall()
else:
res = conn.cursor().execute(query, params).fetchall()
except Error as e:
print(e)
return res
def create_connection(db_file):
"""
Create a database connection to a SQLite database
"""
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
def create_table(conn):
"""
Create a database table
"""
sql_create_table = """ CREATE TABLE IF NOT EXISTS 'stock_data' (
id INTEGER PRIMARY KEY AUTOINCREMENT,
uid VARCHAR(255) NOT NULL,
tim TIMESTAMP NOT NULL,
popularity INTEGER NOT NULL,
price FLOAT NOT NULL,
weight FLOAT DEFAULT NULL,
comp_id INTEGER DEFAUL NULL
);
"""
sql_create_comp_table = """ CREATE TABLE IF NOT EXISTS 'compositions' (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tim TIMESTAMP NOT NULL,
value FLOAT NOT NULL
);
"""
with conn:
try:
c = conn.cursor()
c.execute(sql_create_table)
c.execute(sql_create_comp_table)
except Error as e:
print(e)
def index_insert(conn, id, timestamp, popularity, price, weight):
"""
Insert an SQL entry for an id
"""
query = """ INSERT INTO 'index_data'
(id, tim, popularity, price, weight)
VALUES (?, ?, ?, ?, ?);
"""
execute(conn, query, (id, timestamp, popularity, price, weight))
def index_insert_many(conn, data):
query = """ INSERT INTO 'index_data'
(id, tim, popularity, price, weight)
VALUES """
if isinstance(data, dict):
timestamp = round(time.time())
for instrument, result in data.items():
items = ["'" + instrument + "'", str(timestamp), str(result["pop"]), str(result["price"]), str(0)]
query += "(" + ', '.join(items) + "), "
query = query[:-2] + ";"
elif isinstance(data, list):
pass
execute(conn, query)
def index_update(conn, id, timestamp, weight):
"""
Update an entry's weight
"""
tim1 = timestamp - (timestamp % 3600)
tim2 = tim1 + 3600
query = "UPDATE index_data SET weight = ? WHERE id = ? AND tim BETWEEN {} AND {}".format(tim1, tim2)
execute(conn, query, (weight, id))
def insert_value(conn, timestamp, value):
query = "INSERT INTO index_value (tim, value) VALUES (?, ?);"
execute(conn, query, (int(timestamp), value))
def get_all_values(conn):
query = "SELECT * FROM index_value"
return execute(conn, query)