-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
49 lines (34 loc) · 1.87 KB
/
database.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
import sqlite3
import typing
class WorkspaceData:
def __init__(self):
self.conn = sqlite3.connect("database.db")
self.conn.row_factory = sqlite3.Row # Makes the data retrieved from the database accessible by their column name
self.cursor = self.conn.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS watchlist (symbol TEXT, exchange TEXT)")
self.cursor.execute("CREATE TABLE IF NOT EXISTS strategies (strategy_type TEXT, contract TEXT,"
"timeframe TEXT, balance_pct REAL, take_profit REAL, stop_loss REAL, extra_params TEXT)")
self.conn.commit() # Saves the changes
def save(self, table: str, data: typing.List[typing.Tuple]):
"""
Erase the previous table content and record new data to it.
:param table: The table name
:param data: A list of tuples, the tuples elements must be ordered like the table columns
:return:
"""
self.cursor.execute(f"DELETE FROM {table}")
table_data = self.cursor.execute(f"SELECT * FROM {table}")
columns = [description[0] for description in table_data.description] # Lists the columns of the table
# Creates the SQL insert statement dynamically
sql_statement = f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({', '.join(['?'] * len(columns))})"
self.cursor.executemany(sql_statement, data)
self.conn.commit()
def get(self, table: str) -> typing.List[sqlite3.Row]:
"""
Get all the rows recorded for the table.
:param table: The table name to get the rows from. e.g: strategies, watchlist
:return: A list of sqlite3.Rows accessible like Python dictionaries.
"""
self.cursor.execute(f"SELECT * FROM {table}")
data = self.cursor.fetchall()
return data