Setup SQLite, Import and Export from CSV
SQLite is a small, fast, reliable, self-contained, fully featured SQL database, but unlike most database systems, it stores databases in simple files on disk. It is popular in projects like mobile phone apps that need a small local database that can be transferred easily, and Python also has a native library to read and write SQLite database files. SQLite was originally programmed in C, so you can download compiled executables for its command line environment. That environment has convenient commands for importing tables from CSV files, so we will practice that and run some queries.
Optional Reading
Some students in this class have already taken the Databases class, and some have not. We will be doing fairly simple queries. If you are unfamiliar with SQL, please use the optional text "Sam's Teach Yourself SQL in Ten Days" as a reference guide for any database queries you may wish to do. We will teach sufficient SQL for the project in class.
Download SQLite on Your Local Machine
Go to https://sqlite.org/download.html and download an appropriate zip file that includes the command-line interface. When unzipped, place the executable files in a path directory (the "path" command should show the system path). I prefer to add mine to the Python directory in my path.
Download CSV Data
Download the following CSV spreadsheets into any directory:
Import CSV DATA into a SQLite Database
Create a database called batman.db by running the following command:
Now import the CSV files with the following commands:
.mode csv
.import characters.csv characters
.import movies.csv movies
.import character_movie.csv character_movie
Look at the tables and schema using these commands:
.tables
.schema
.schema characters
.schema movies
.schema character_movie
Quit using the .quit command:
Re-open from the command line:
Run Simple SQL Queries that Read Data
Show table data using these queries:
SELECT name FROM characters;
SELECT title FROM movies;
SELECT title, year FROM movies WHERE year > 2010 ORDER BY year;
SELECT title, year FROM movies WHERE year <= 2010 ORDER BY year DESC;
SELECT title, year FROM movies WHERE year <= 2010 ORDER BY year DESC LIMIT 0,2;
SELECT title, year FROM movies WHERE year <= 2010 ORDER BY year DESC LIMIT 2,2;
Run Simple SQL Queries that Write Data
Write data using these queries:
UPDATE characters SET alias='The Butler' WHERE id=2;
INSERT INTO characters (name, alias, affiliation) VALUES ('Don Mitchell, Jr.','The Mayor','Other');
Use SELECT queries to verify that the changes were made.
Run More Complex SQL Queries that Read Data
Use queries like this to get data from multiple tables:
SELECT name FROM (characters c JOIN character_movie cm ON c.id=cm.character_id) WHERE movie_id=2
SELECT name, title, year FROM ((characters c JOIN character_movie cm ON c.id=cm.character_id) JOIN movies m ON m.id=cm.movie_id) WHERE movie_id=2 ORDER BY name
Save Query Results to CSV
Output the last query's results into a CSV file using the following:
.mode csv
.output result.csv
SELECT name, title, year FROM ((characters c JOIN character_movie cm ON c.id=cm.character_id) JOIN movies m ON m.id=cm.movie_id) WHERE movie_id=2 ORDER BY name
.output stdout
.quit