Does sqlite have an equivalent to MySQL's --execute? - sqlite

I want to pipe the output of a SQLite query. With MySQL, I would do mysql --execute "MYQUERY". How can I achieve the same thing with SQLite?

The optional second argument to the sqlite3 command is SQL to execute. So sqlite3 path/to/my/db "MYQUERY" does it.

Related

How can I quit sqlite from a command batch file?

I am trying to create a sealed command for my build pipeline which inserts data and quits.
So far I have created my data files
things-to-import-001.sql and 002 etc, which contains all the INSERT statements I'd like to run, with a file per table.
I have created a command file to run them
-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.quit
However when I run my command
sqlite3 -init ./import-all.sql ./database.sqlite
..the data is inserted, but the program remains running and shows the sqlite> prompt, despite the .quit command. I have also tried using .exit 0.
From the sqlite3 --help
-init FILENAME read/process named file
Docs: https://www.sqlite.org/cli.html#reading_sql_from_a_file
How can I tell sqlite to exit once my inserts have finished?
I have managed to find a dirty workaround for this issue.
I have updated my import file to include a bad command, and executed using -bail to quit on first error.
-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.fakeErrorToQuitWithBail
Then you can execute with
sqlite3 -init import-all.sql -bail
and it should quit with
Error: unknown command or invalid arguments: "fakeErrorToQuitWithBail". Enter ".help" for help
Try using ".exit" at the place of ".quit". For some reason SQLite dont doccumented this commands.
https://www.tutorialspoint.com/sqlite/sqlite_commands.htm

Why am I getting: database is locked, in an SQLite3 script?

I'm getting an error when running an SQLite script.
--drop use table before replacing it
DROP TABLE IF EXISTS db.use;
--Create the use table in the saved database
CREATE TABLE db.use AS SELECT * FROM use2; -- this is the line that generates the error: Error: near line 145: database is locked
Are these two statements run asynchronously or something? I don't understand what's causing the error, but I'm wondering if it has to do with that.
Might there be a way to run the script in a lock-step manner, i.e. non-asynchronously?
This is how I was running the command: sqlite3 --init script_name.sql dbname.db, and elsewhere in the script I had an ATTACH statement reading the same database dbname.db. Essentially reading the same file twice.
The way I solved this was by executing the script in the sqlite3 shell:
sqlite3> .read script_name.sql
Have you tried to add a commit statement after the drop statement?
I think that would make sure the create table statement run after the drop statement is totally done.

No such function: sqlcipher_export()

i using start terminal
-macbook:sqlTest user1$ sqlite3 sqlTest.sqlite
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> ATTACH DATABASE 'encrypted.sqlite' AS encrypted KEY 'testkey';
sqlite> SELECT sqlcipher_export('encrypted');
Error: no such function: sqlcipher_export
sqlite>
what makes no such function: sqlcipher_export?
As answered on the mailing list:
The first step is to build the sqlcipher command line tool, as described here:
http://sqlcipher.net/introduction/
Once you have done this, you should run the command like this:
$ ./sqlcipher sqlTest.sqlite
or
$ /full/path/to/sqlcipher/sqlcipher sqlTest.sqlite
On unix systems, if you don't provide an explicit path for a command, the system will look for the program in $PATH. On OSX, the system ships with a sqlite3 command, so you've probably been using that instead of the version compiled with SQLCipher. Please let us know if that resolves the problem. Thanks!

How to vacuum sqlite database?

I want to know how to vacuum sqlite database.
I tried a syntax MANUAL VACUUM command for the whole database from command prompt:
$sqlite3 database_name "VACUUM;";
But it's giving error as:
near "database_name": syntax error.
and also AUTO VACUUM:
PRAGMA auto_vacuum = INCREMENTAL;
And tried it for a particular table as:
VACUUM table_name;
But no result.
You don't to specify the table name in the syntax. Only VACUUM works.
Also, it will clean the main database only and not any attached database files.
For more info, refer to the SQLite documentation.
Give the command like this:
$sqlite3 database_name 'VACUUM;'
As a matter of fact, this is the way to do also other queries from command line:
$sqlite3 database_name 'select * from tablename;'
You can use the full path to the db:
$sqlite3 /path/to/db/foo.db 'VACUUM;'
Run the command:
VACUUM;
if you use DB Browser for Sqlite or
open Sqlite from command prompt:
cd C:\your_folder
C:\Users\your_user\AppData\Local\Android\Sdk\platform-tools\sqlite3.exe -line your_db_name.db
and run
VACUUM;

R Postgres and shortcut commands?

Do postgres shortcuts like \d+ tablename work with RPostgreSQL?
If I try to run 'em I get a syntax error: Error: '\d' is an unrecognized escape in character string starting "\d".
I tried escape it, but did not figure it out. RPostgreSQL itself works -- I can access and query my database.
No. These are "psql metacommands" and only recognised by the psql command-line interpreter. Only SQL commands can be passed through RPostgreSQL to the Postgres database.

Resources