Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60854615
SQLCreate_Pandas.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, May 2, 23:31
Size
1 KB
Mime Type
text/x-python
Expires
Sat, May 4, 23:31 (2 d)
Engine
blob
Format
Raw Data
Handle
17410905
Attached To
rSQLVAL SQLValidation
SQLCreate_Pandas.py
View Options
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...
\n\n
{e}"
)
return
table
## 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
Log In to Comment