Skip to content

Instantly share code, notes, and snippets.

@theholy7 theholy7/sql_to_df.py
Last active Sep 8, 2018

Embed
What would you like to do?
psycopg2, pandas and python
import os
import pandas.io.sql as sqlio
import psycopg2
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
SQL_USER = os.environ.get('SQL_USER')
SQL_PASS = os.environ.get('SQL_PASS')
SQL_HOST = os.environ.get('SQL_HOST')
SQL_PORT = os.environ.get('SQL_PORT')
SQL_DB = os.environ.get('SQL_DB')
conn = psycopg2.connect(
dbname=SQL_DB,
user=SQL_USER,
password=SQL_PASS,
port=SQL_PORT,
host=SQL_HOST
)
SQL_QUERY = "SELECT * FROM test_table WHERE id = ANY(%s)"
test_ids = [1, 2, 3]
result_df = sqlio.read_sql_query(SQL_QUERY, params=(test_ids,), conn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.