-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
74 lines (62 loc) · 2.8 KB
/
db.py
File metadata and controls
74 lines (62 loc) · 2.8 KB
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
import sqlite3
import time
import datetime
class Database:
_path = 'database.db'
_conn = None
_c = None
def __init__(self):
self._conn = sqlite3.connect(self._path)
self._c = self._conn.cursor()
self._create_tables()
def _create_tables(self):
"""Creates the tables.
Creates the tables currency and val unless they already exist.
"""
self._c.execute("CREATE TABLE IF NOT EXISTS currency ( "
"id INTEGER PRIMARY KEY, "
"name CHAR(50) NOT NULL, "
"type CHAR(50) NOT NULL, "
"slug CHAR(50) UNIQUE NOT NULL)")
self._c.execute("CREATE TABLE IF NOT EXISTS val ( "
"id INTEGER PRIMARY KEY, "
"price_usd REAL NOT NULL, "
"price_btc REAL NOT NULL, "
"volume_usd REAL NOT NULL, "
"market_cap_usd REAL NOT NULL, "
"available_supply REAL NOT NULL, "
"datetime DATETIME NOT NULL, "
"currency_slug CHAR(50) NOT NULL, "
"FOREIGN KEY(currency_slug) REFERENCES currency(slug) "
"ON DELETE CASCADE ON UPDATE CASCADE)")
self._conn.commit()
def _val_entry(self, name, slug, type, price_usd, price_btc, volume_usd,
market_cap_usd, available_supply, datetime):
"""Enter new data into the database."""
# If currency does not exist yet, insert entry
self._c.execute("SELECT * FROM currency WHERE slug = ?", (slug,))
if (self._c.fetchone() is None):
self._c.execute("INSERT INTO currency (name, slug, type) VALUES (?, ?, ?)",
(name, slug, type))
self._conn.commit()
# Insert values
self._c.execute("INSERT INTO val ("
"price_usd, price_btc, volume_usd, market_cap_usd, "
"available_supply, datetime, currency_slug) "
"VALUES(?, ?, ?, ?, ?, ?, ?)", (
price_usd, price_btc, volume_usd, market_cap_usd,
available_supply, datetime, slug))
self._conn.commit()
def batch_entry(self, data, name, type):
for entry in data:
self._val_entry(name,
entry['slug'],
type,
entry['price_usd'],
entry['price_btc'],
entry['volume_usd'],
entry['market_cap_by_available_supply'],
entry['est_available_supply'],
entry['time'])
def __del__(self):
self._conn.close()