Importing Relational Databases Flashcards
Importing Data in Python (Part 1)
1
Q
module for SQL engines
A
from sqlalchemy import create_engine
2
Q
creating an engine
A
engine = create_engine(‘sqlite(dbtype):///filename’)
3
Q
get table names from database
A
engine.table_names()
4
Q
opening a connection
A
con = engine.connect()
5
Q
perform an SQL query
A
rs = con.execute(‘Query’)
6
Q
save results of query to dataframe
A
pd.DataFrame(rs.fetchall())
7
Q
SQL to DataFrame step by step
A
- import create_engine from sqlalchemy
- create an engine
- open connection
- perform SQL query
- save results to DataFrame
8
Q
Query form
A
‘SELECT ColumnA, Column B (or *) FROM Table WHERE…(optional) ORDER BY…’
9
Q
querying SQL with pandas
A
pd.read_sql_query(‘query’, engine)
10
Q
inner join
A
exploit table relations: SELECT * FROM X INNER JOIN Y on *.X = *.Y