How to vacuum sqlite database? - sqlite

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;

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.

sqlite3 disk I/O error on cli

sqlite version 3.6.20, running through VNC.
Starting sqlite3 cli session. When trying to run commands ".tables", ".databases", "create table" I get "Error: disk I/O error". I don't know how to get more accurate description. I want to write in my home directory where I have permissions.
I tried some suggested fixes in .sqliterc with journal mode and temp storage - they do not help. Some commands like "PRAGMA synchronous = OFF;" also cause disk io error.
.output /dev/null
PRAGMA journal_mode = MEMORY;
PRAGMA locking_mode = EXCLUSIVE;
PRAGMA temp_store_directory = '/home/username/tmp';
How to find out more about error and solve this?
It was VMWARE related error. Solution: move files to /tmp. Sqlite works there.
It is described here https://dba.stackexchange.com/questions/93575/sqlite-disk-i-o-error-3850

When using clojure's korma sqlite3 helpers, what's the default path for the sqlite3 database?

When using korma.db, defdb can take a sqlite3 helper to establish a connexion to a sqlite3 database. However, I've tried placing the database on the root of the project directory, alongside project.clj, and on the resources directory, but when I try to use the db I get:
Failure to execute query with SQL:
SELECT "examples".* FROM "examples" :: []
SQLException:
Message: [SQLITE_ERROR] SQL error or missing database (no such table: examples)
Needless to say my sqlite database contains an examples table. When trying to do this, I get a sqlite.db file of zero bytes placed on the root project dir.
I'm doing this from lein repl within the project, by the way.
Edit: This is what I do when it fails:
(use 'korma.db)
(defdb db (sqlite3 {:db "filename.db"}))
(use 'korma.core)
(defentity examples)
(select examples)
Just in case anybody is wondering or runs into this...
Using version [korma "0.4.2"]
and [org.xerial/sqlite-jdbc "3.7.15-M1"]
in my project.clj:
My project structure looks like:
root/project.clj
root/db/dev.sqlite3
root/src/...
and this is how I use korma to access the db:
(use 'korma.db)
(defdb mydb {:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname "db/dev.sqlite3"})
Basically, using subname, I'm able to search in the root of the lein project. I added db/ in the subname per my dir structure above.

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

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.

Resources