-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_storer.py
59 lines (49 loc) · 2.07 KB
/
data_storer.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
import sqlite3
import pandas as pd
# Function to create the events table if it does not exist
def create_table(db_name='economic_events.db'):
"""
Create the events table if it does not exist.
Parameters:
db_name (str): The name of the database file. Default is 'economic_events.db'.
"""
conn = sqlite3.connect(db_name)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS events
(Date TEXT, Time TEXT, Currency TEXT, Volatility TEXT, Event TEXT, Forecast TEXT, Previous TEXT)''')
conn.commit()
conn.close()
# Function to store events in the database
def store_events(events, db_name='economic_events.db'):
"""
Store events in the database.
Parameters:
events (list of dict): A list of events, where each event is a dictionary containing event details.
db_name (str): The name of the database file. Default is 'economic_events.db'.
"""
conn = sqlite3.connect(db_name)
c = conn.cursor()
# Add only new events
for event in events:
c.execute('''SELECT * FROM events WHERE Date=? AND Time=? AND Currency=? AND Event=?''',
(event['Date'], event['Time'], event['Currency'], event['Event']))
if not c.fetchone():
c.execute('INSERT INTO events (Date, Time, Currency, Volatility, Event, Forecast, Previous) VALUES (?, ?, ?, ?, ?, ?, ?)',
(event['Date'], event['Time'], event['Currency'], event['Volatility'], event['Event'], event['Forecast'], event['Previous']))
conn.commit()
conn.close()
# Function to retrieve events from the database
def get_events(db_name='economic_events.db'):
"""
Retrieve events from the database.
Parameters:
db_name (str): The name of the database file. Default is 'economic_events.db'.
Returns:
pandas.DataFrame: A DataFrame containing all events from the database.
"""
conn = sqlite3.connect(db_name)
df = pd.read_sql_query('SELECT * FROM events', conn)
conn.close()
return df
# Initialize the database and create the table if it does not exist
create_table()