Using Pandas to connect to SQL and write SQL queries
Pandas and The Hello World of SQL Queries!
Here, you'll take advantage of the power of pandas
to write the results of your SQL query to a DataFrame in one swift line of Python code!
You'll first import pandas
and create the SQLite 'Chinook.sqlite'
engine. Then you'll query the database to select all records from the Album
table.
Recall that to select all records from the Orders
table in the Northwind database, Hugo executed the following command:
df = pd.read_sql_query("SELECT * FROM Orders", engine)
# Import packagesfrom sqlalchemy import create_engineimport pandas as pd
# Create engine: engineengine = create_engine('sqlite:///Chinook.sqlite')
# Execute query and store records in DataFrame: dfdf = pd.read_sql_query("Select * FROM Album", engine)
# Print head of DataFrameprint(df.head())
# Open engine in context manager and store query result in df1with engine.connect() as con: rs = con.execute("SELECT * FROM Album") df1 = pd.DataFrame(rs.fetchall()) df1.columns = rs.keys()
# Confirm that both methods yield the same resultprint(df.equals(df1))
Comments
Post a Comment