Page MenuHomec4science

SQLCreate_Pandas.py
No OneTemporary

File Metadata

Created
Thu, Mar 28, 20:32

SQLCreate_Pandas.py

import sqlite3
import pandas
import hashlib
import pickle
from sqlalchemy import create_engine
from pandas.io.sql import DatabaseError
"""
Utility functions
"""
def generate_hash(dataframe):
return hashlib.md5(pickle.dumps(dataframe)).hexdigest()
def run_query(q):
conn = sqlite3.connect("stork-mooc.sqlite")
return pandas.read_sql_query(q, conn)
def display_observations_table():
query = "select * from observations"
table = pandas.DataFrame()
try:
table = run_query(query)
except DatabaseError as e:
print(f"Looks like the 'observations' table does not exist...")
return table
def delete_observations_table():
query = "DROP TABLE observations"
conn = sqlite3.connect("stork-mooc.sqlite")
try:
conn.cursor().execute(query)
conn.commit()
print("Table deleted.")
except sqlite3.OperationalError as e:
print(f"Looks like the 'observations' table does not exist...")
## Alternative way to send queries with auto-closing connection
## Issue: the hash obtained for the returned table does not match...
def display_observations_table_engine():
engine = create_engine("sqlite:///stork-mooc.sqlite")
conn = sqlite3.connect("stork-mooc.sqlite")
table = pandas.DataFrame()
try:
with engine.connect() as conn, conn.begin():
table = pandas.read_sql_table("observations", conn)
except ValueError:
print("Looks like the 'observations' table does not exist...")
return table

Event Timeline