LcSalazar
LcSalazar

Reputation: 16821

Is it possible to execute a query against a sqlite database with a single line terminal command?

I want to perform an insert in a sqlite database, programatically from my app. I don't want to create a Database adapter, or any of that... Instead, since it's a simple statement, I thought about running a shell command.

Normally, I would do like:

# sqlite3 database.db

And then:

sqlite3> INSERT INTO table VALUES (something);

But, I would like to know if it is possible to run a query against a sqlite database in a single command?

If so, I could use the Runtime.getRuntime().exec('command') method from within my app.

Upvotes: 2

Views: 1873

Answers (2)

Adobe
Adobe

Reputation: 13467

In case you need to exec multiple statements, and you are on bash (or any other POSIX shell):

sqlite3 data.db3 << EOF
SELECT * FROM table1;
SELECT * FROM table2;
EOF

Upvotes: 0

laalto
laalto

Reputation: 152787

Yes there is. Just put the SQL as an argument on the command line:

sqlite3 database.db "INSERT INTO table VALUES (something)"

Note that not all Android devices ship with a sqlite3 executable. Consider doing the database operations in your code instead.

Upvotes: 4

Related Questions