datamade / data-analysis-guidelines

📒 Analyzing Data, the DataMade Way
MIT License
36 stars 4 forks source link

Add basic setup for database-backed analysis #7

Open hancush opened 6 years ago

hancush commented 6 years ago

pip install sqlalchemy and psycopg2.

Then, in your Pweave markdown (.pmd) or Pweave TeX (.ptexw) file:

Import pandas and sqlalchemy, and connect to your database.

<<>>=
import pandas as pd

from sqlalchemy import create_engine

DATABASE_FORMAT = 'postgresql://{user}:{pw}@{host}:{port}/{name}'

DATABASE_OPTIONS = {
    'user': 'postgres',
    'host': 'localhost',
    'pw: '',
    'port': 5432,
    'name': 'YOUR DATABASE',  # Change this to the name of your database
}

DATABASE_CONNECTION = DATABASE_FORMAT.format(**DATABASE_OPTIONS)

engine = create_engine(DATABASE_CONNECTION)
conn = engine.connect()
@

Issue SQL to your database with pandas.read_sql(). The first argument can be a SQL query, or the name of a table (if you want to select the entire thing). The second argument is your database connection.

<<>>=
pd.read_sql('SELECT * FROM my_table', conn)  # Returns query result as pandas DataFrame.
@

At the very end of your file, close your connection.

<<>>=
conn.close()
@
hancush commented 6 years ago

Update: You can just pass the engine