22title : Accessing SQLite Databases Using Python and Pandas
33teaching : 20
44exercises : 25
5-
5+ questions :
6+ - " FIXME"
67objectives :
7- - Use the sqlite3 module to interact with a SQL database.
8- - Access data stored in SQLite using Python.
9- - Describe the difference in interacting with data stored as a CSV file versus in SQLite.
10- - Describe the benefits of accessing data using a database compared to a CSV file.
8+ - " Use the sqlite3 module to interact with a SQL database."
9+ - " Access data stored in SQLite using Python."
10+ - " Describe the difference in interacting with data stored as a CSV file versus in SQLite."
11+ - " Describe the benefits of accessing data using a database compared to a CSV file."
12+ keypoints :
13+ - " FIXME"
1114---
1215
1316## Python and SQL
@@ -30,7 +33,7 @@ perform all kinds of operations with `.execute()`.
3033
3134[ sqlite3 ] : https://docs.python.org/3/library/sqlite3.html
3235
33- ``` python
36+ ~~~
3437import sqlite3
3538
3639# Create a SQL connection to our SQLite database
@@ -44,7 +47,8 @@ for row in cur.execute('SELECT * FROM species;'):
4447
4548# Be sure to close the connection
4649con.close()
47- ```
50+ ~~~
51+ {: .language-python}
4852
4953### Queries
5054
@@ -53,7 +57,7 @@ retrieving data based on some search parameters. Use a SELECT statement string.
5357The query is returned as a single tuple or a tuple of tuples. Add a WHERE
5458statement to filter your results based on some parameter.
5559
56- ``` python
60+ ~~~
5761import sqlite3
5862
5963# Create a SQL connection to our SQLite database
@@ -71,15 +75,16 @@ cur.fetchone()
7175
7276# Be sure to close the connection
7377con.close()
74- ```
78+ ~~~
79+ {: .language-python}
7580
7681## Accessing data stored in SQLite using Python and Pandas
7782
7883Using pandas, we can import results of a SQLite query into a dataframe. Note
7984that you can use the same SQL commands / syntax that we used in the SQLite
8085lesson. An example of using pandas together with sqlite is below:
8186
82- ``` python
87+ ~~~
8388import pandas as pd
8489import sqlite3
8590
@@ -91,7 +96,8 @@ df = pd.read_sql_query("SELECT * from surveys", con)
9196print(df.head())
9297
9398con.close()
94- ```
99+ ~~~
100+ {: .language-python}
95101
96102## Storing data: CSV vs SQLite
97103
@@ -118,7 +124,7 @@ benchmarks]).
118124
119125We can also us pandas to create new tables within an SQLite database. Here, we run we re-do an excercise we did before with CSV files using our SQLite database. We first read in our survey data, then select only those survey results for 2002, and then save it out to its own table so we can work with it on its own later.
120126
121- ``` python
127+ ~~~
122128import pandas as pd
123129import sqlite3
124130
@@ -134,7 +140,8 @@ surveys2002 = surveys_df[surveys_df.year == 2002]
134140surveys2002.to_sql("surveys2002", con, if_exists="replace")
135141
136142con.close()
137- ```
143+ ~~~
144+ {: .language-python}
138145
139146> ## Challenge - Saving your work
140147>
0 commit comments