bashsqlite

Non-interactive SQLite3 usage from bash script


I see plenty of examples showing how to use the sqlite3 interactive shell, e.g.:

$ sqlite3
$ sqlite3> SELECT * from x;

but I am looking for a way to create a table in a SQLite3 database with a bash script, aka, non-interactively.

For example, the following doesn't seem to work (compare last 2 characters with accepted answer), it remains interactive:

#!/bin/bash
sqlite3 test.db  "create table n (id INTEGER PRIMARY KEY,f TEXT,l TEXT);"
sqlite3 test.db  "insert into n (f,l) values ('john','smith');"
sqlite3 test.db  "select * from n";

Solution

  • Looks like it's as simple as

    #!/bin/bash
    sqlite3 test.db  "create table n (id INTEGER PRIMARY KEY,f TEXT,l TEXT);"
    sqlite3 test.db  "insert into n (f,l) values ('john','smith');"
    sqlite3 test.db  "select * from n;"
    

    (last 2 characters swapped) from https://mailliststock.wordpress.com/2007/03/01/sqlite-examples-with-bash-perl-and-python/