Shell Session
You'll leave the clicking behind and drive SQLite from the command line: import a file, run a saved script, send results to a CSV, and dump the whole database to text.
About
Every database has a command-line shell, and the one for SQLite is seven letters and already installed on a Mac and most Linux machines. The shell is where you stop treating SQL as something you type into a box and start treating it as something you save, run and repeat. This badge is an evening with the dot commands that make the shell useful, ending with .dump, which turns a whole database into a text file you can rebuild it from, and which is the seed of every backup badge further up the track. On Windows, download sqlite3.exe from sqlite.org or use the Windows Subsystem for Linux. What counts is each step done in the terminal, with the command and its output visible together.
At a glance
- Per requirement
- 15 XP
- Completion bonus
- 75 XP
- Requirements
- 5
- Takes
- One evening
- Skill level
- Beginner (done it a few times)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Open a new database file from the shell and show its version. Run sqlite3 practice.db, then SELECT sqlite_version(); and .quit. The file now exists on disk even though it has no tables in it yet. If the version is older than 3.25, update it, because the window functions further up the track need that. Take a screenshot of the terminal. Photo
-
2
Import a CSV with .mode csv and .import, then check the row count and the schema. Type .mode csv, then .import --csv trips.csv trips (older versions skip the --csv flag), then SELECT COUNT(*) FROM trips; and .schema trips. When you let .import create the table, every column arrives as TEXT, which is why the Null Hunt badge exists. Create the table with real types first if you'd rather. Take a screenshot of the commands and their output. Photo
-
3
Make a query readable with .headers on and .mode column, then send the same result to a file with .output. Run .headers on and .mode column, then your query, then .mode csv, .output result.csv, the query again, and .output stdout to get your screen back. Open result.csv in a text editor to see it landed. Take a screenshot of the terminal and the file. Photo
-
4
Put five queries in a .sql file and run the file from the shell in one command. Either sqlite3 practice.db < queries.sql from your normal prompt, or .read queries.sql from inside the shell. A comment with two hyphens above each query says what question it answers. This is the moment SQL becomes something you can rerun next month without remembering it. Take a screenshot of the file and the output. Photo
-
5
Dump the database to a text file, move the database away, and rebuild it from the dump. Run sqlite3 practice.db .dump > backup.sql, then move practice.db somewhere safe rather than deleting it, then sqlite3 rebuilt.db < backup.sql and a COUNT(*) that matches the original. Open backup.sql in a text editor and read it: it's just CREATE and INSERT statements, which is all a database is. Film the sequence, or take a screenshot of each step. Photo or video