Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command-line SQLite 3 query and exit

We can use the -cmd option with sqlite3 to run a query, but then sqlite3 opens the database and waits in there for interactive input. How can we run a query on sqlite3 from the command line and exit?

like image 211
farmir Avatar asked Jul 02 '15 03:07

farmir


People also ask

How do I exit SQLite 3?

Terminate the sqlite3 program by typing your system End-Of-File character (usually a Control-D). Use the interrupt character (usually a Control-C) to stop a long-running SQL statement.

How do I get out of SQLite command line?

Ctrl + D will get you out of the SQLite 3 database command prompt. That is: hold the "Ctrl" button then press the lowercase d key on your keyboard at the same time and you will escape the SQLite 3 command prompt.


3 Answers

Just include the command in quotes after the database file argument.

For example, the following creates a table called abc:

sqlite3 test.db 'create table abc (col0 int)'
like image 120
gbrener Avatar answered Oct 16 '22 09:10

gbrener


You can use the .exit command (1), to exit gracefully:

sqlite3 test.db "select * from abc;" ".exit"

Documentation: Command Line Shell For SQLite.

like image 48
Annoying Technology Avatar answered Oct 16 '22 11:10

Annoying Technology


If you are stuck in a situation where you absolutely "have to" use the -cmd flag when you are running SQLite 3 from the command line, you can use successive blank command to exit.

For example:

sqlite3 test.db "select * from urls;" "" > test.txt

In this example, the "" will cause the SQLite 3 process to exit. (At least it does for me on OS X.)

like image 6
f1lt3r Avatar answered Oct 16 '22 09:10

f1lt3r