You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: opendata-python/README.md
+62Lines changed: 62 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -166,3 +166,65 @@ activities[99].metadata
166
166
...
167
167
}}
168
168
```
169
+
170
+
### Connecting to a PostgreSQL database
171
+
Although having all the Open Data files available as plain files on your computer has advantages (especially for less tech-savvy users), querying the data is slow and can be complicated.
172
+
To overcome this, it is possible to store all the data in a [PostgreSQL](https://www.postgresql.org/) database as well.
173
+
174
+
Setting up PostgreSQL (documentation [here](https://www.postgresql.org/docs/11/tutorial-install.html)) can be hassle, so there is a `docker-compose.yaml` included in this repository that *should* work out of the box by running `docker-compose up` in the directory where the file is stored.
175
+
I am not going into the rabbit hole of explaining how to install docker and docker-compose here (a quick search will yield enough results for that). One comment: On MacOS and Linux installation is mostly painless, on Windows it not always is and I would advice against using docker there.
176
+
As an alternative, you can use a local installation of PostgreSQL (assuming username=opendata, password=password, database name=opendata by default).
177
+
178
+
When PostgreSQL is installed correctly and running, inserting data into the database is as easy as:
179
+
```python
180
+
from opendata import OpenData
181
+
from opendata.db.main import OpenDataDB
182
+
from opendata.models import LocalAthlete
183
+
184
+
od = OpenData()
185
+
opendatadb = OpenDataDB()
186
+
opendatadb.create_tables() # This is only needed once
Please note: This only inserts the athlete into the database, not the activities for this athlete.
193
+
To add al the activities too:
194
+
```python
195
+
for activity in athlete.activities():
196
+
opendatadb.insert_activity(activity, athlete)
197
+
```
198
+
199
+
At this point there are 2 tables in the opendata database: "athletes" and "activities".
200
+
The database schemas for both tables can be viewed [here](opendata/db/models.py).
201
+
202
+
If you are familiar with raw SQL you can query the database directly, but if you prefer to stay in Python land, I got you covered too: Under the hood this library uses the [SQLAlchemy](https://www.sqlalchemy.org/) ORM.
203
+
For some general documentation on how that works, see [here](https://docs.sqlalchemy.org/en/latest/orm/tutorial.html).
204
+
Querying the data is possible using SQLAlchemy's query language (documentation [here](https://docs.sqlalchemy.org/en/latest/orm/query.html)).
205
+
206
+
For example, to get a count of all activities that have power:
0 commit comments