diff options
author | Vincent Ambo <mail@tazj.in> | 2021-12-13T22·51+0300 |
---|---|---|
committer | Vincent Ambo <mail@tazj.in> | 2021-12-13T23·15+0300 |
commit | 019f8fd2113df4c5247c3969c60fd4f0e08f91f7 (patch) | |
tree | 76a857f61aa88f62a30e854651e8439db77fd0ea /users/wpcarro/playbooks/sqlite3.md | |
parent | 464bbcb15c09813172c79820bcf526bb10cf4208 (diff) | |
parent | 6123e976928ca3d8d93f0b2006b10b5f659eb74d (diff) |
subtree(users/wpcarro): docking briefcase at '24f5a642' r/3226
git-subtree-dir: users/wpcarro git-subtree-mainline: 464bbcb15c09813172c79820bcf526bb10cf4208 git-subtree-split: 24f5a642af3aa1627bbff977f0a101907a02c69f Change-Id: I6105b3762b79126b3488359c95978cadb3efa789
Diffstat (limited to 'users/wpcarro/playbooks/sqlite3.md')
-rw-r--r-- | users/wpcarro/playbooks/sqlite3.md | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/users/wpcarro/playbooks/sqlite3.md b/users/wpcarro/playbooks/sqlite3.md new file mode 100644 index 000000000000..aec87f0b59ee --- /dev/null +++ b/users/wpcarro/playbooks/sqlite3.md @@ -0,0 +1,115 @@ +# SQLite3 + +Creating a reference for SQLite that I can access when I'm offline +(e.g. traveling in an airplane). + +## Benefits + +I enjoy using SQLite because it's lightweight and simple. Instead of networking +microservices, I can oftentimes just create a simple `db.sqlite3` file and get +significant mileage without much tooling overhead. + +## Limitations + +SQLite has some limitations; here are some of the limitations that I have encountered. + +- SQLite **disables** support for `FOREIGN KEY` by default. Enable it with: + +``` +sqlite> PRAGMA foreign_keys = ON; +``` + +- SQLite has no `BOOLEAN` type; it uses 0 and 1 instead. + +``` +sqlite> SELECT TRUE; +TRUE +---------- +1 +sqlite> SELECT FALSE; +FALSE +---------- +0 +``` + +- SQLite has no `DATETIME` type; it uses `TEXT` instead. + +``` +sqlite> SELECT datetime('now'); +datetime('now') +------------------- +2020-07-26 09:52:32 +``` + +## Reference + +The following should serve as a useful reference for working with SQLite. + +### Schema + +```sql +CREATE TABLE IF NOT EXISTS Movies ( + title TEXT NOT NULL, + year INTEGER, + PRIMARY KEY (title) +); + +ALTER TABLE Movies ADD COLUMN rating DEFAULT 0.0; + +DROP TABLE Movies; +``` + +### Queries + +The following queries should come in handy as a reference: + +``` +sqlite> -- I'm using an intentionally incorrect date here for the subsequent UPDATE. +sqlite> INSERT INTO Movies (title, year) VALUES ('Toy Story 3', 2100); +sqlite> SELECT * FROM Movies WHERE year IS NULL; +sqlite> UPDATE Movies SET year = 2010 WHERE title = 'Toy Story 3'; +sqlite> -- % is like .* in a regex +sqlite> DELETE FROM Movies WHERE title LIKE 'Toy Story%'; +``` + +## Command Line + +- Create a `~/.sqliterc` file with the following contents: + +``` +.mode column +.headers on +``` + +- To start an interactive session: + +```shell +$ sqlite3 db.sqlite3 +``` + +- To create a SQLite database from a `.sql` file: + +```shell +$ sqlite3 db.sqlite3 <db.sql +``` + +- To reload changes to a `.sql` file while in an interactive session: + +``` +sqlite> .read db.sql +``` + +## Miscellaneous + +- For a web-browser-based SQLite viewer, run the following: + +```shell +$ sqlite_web db.sqlite3 +``` + +- To import a CSV: + +``` +sqlite> .mode csv <table-name> +sqlite> .import path/to/file.csv <table-name> +``` |