-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_to_database.py
48 lines (35 loc) · 1.04 KB
/
export_to_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
import sqlite3
import time
from merge_coin_dicts import get_coin_info
#grabbing coin_info
coin_info = get_coin_info()
#creation / connect of the database in current directory
coin_db = sqlite3.connect('cryptocurrencies.db')
#creation of a cursor instance
c = coin_db.cursor()
#store time as unix time for database
current_time = time.time()
#error check for invalid cryptocurrency index length
if len(coin_info) < 100:
print("Error: Invalid Coin Info Dictionary Length")
exit()
#for all coins in summary dict
for coin in coin_info:
#record to be added into database
coin_record = [(coin, coin_info[coin]["symbol"], coin_info[coin]["rank"], coin_info[coin]["price"], current_time)]
#inserting values into table
c.executemany("""INSERT INTO coins VALUES(
?,
?,
?,
?,
?
);""", coin_record)
#test output
#print(coin_record)
#commits changes to database
coin_db.commit()
#close connection
coin_db.close()
#test output
#print(coin_info)