* Non-interactive * using SQLite3 from bash script

I see many examples showing how to use the sqlite3 interactive shell, for example:

$ sqlite3
$ sqlite3> SELECT * from x;

but I'm looking for a way to create a table in a SQLite3 database using a bash script, aka, non-interactive - does anyone know how to do this?

For example, it does n't seem to work , 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";

Also one question - does it ever help “wake SQLite3” by invoking “sqlite3” as a background process - or does it almost always run in the background on MacOS and Linux?

+4
source share
3 answers

It seems 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";

from https://mailliststock.wordpress.com/2007/03/01/sqlite-examples-with-bash-perl-and-python/

0

, , sqlite3 , , :

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

, , bash, "/bin/sh" shebang .

+2

You can turn off interactive mode with the -batch option:

sqlite3 -batch test.db "create table n (id INTEGER PRIMARY KEY,f TEXT,l TEXT);"
+1
source

Source: https://habr.com/ru/post/1669806/


All Articles