I have a Sqlite database file that i modify it by dropping a huge database table that contains thousands of records but the file size remains as it before dropping the table. it seems that it disconnect pointers to data . any ideas how to drop table and make a deep deletion of it in the underlying sqlite file .??
To reclaim space, use the vacuum statement.
From a sqlite prompt
sqlite> VACUUM;
From a shell prompt.
$ sqlite3 filename 'VACUUM;'
Related
Is it possible to create an empty sqlite3 database from the command line (e.g. sqlite3 <someoption> dbname) which would create a database file for me empty of any tables so that I can access it from a different SQL editor?
Currently, when I do sqlite3 dbname, I get a sqlite prompt from which I can do CREATE TABLE ... but if I don't, when I exit, no file is created. So I am looking for a single command which would create an empty database for me without a need to create at least one table within that step.
Use the VACUUM command to create a valid empty SQLite database file, including the root database page and the database header.
sqlite3 file.db "VACUUM;"
I don't think there is a way to do that in just one statement.
In Linux I would workaround it this way:
sqlite3 aFile.db "create table aTable(field1 int); drop table aTable;"
This will automatically create the needed file with the table in it and then drop it leaving the database file without tables. That's the closest thing I know.
Anyway, I think most editors will even accept an empty file too. Give that a try.
The simple way is in bash, use command
touch file.db
This will just create a 0 size file and can be used as an empty sqlite file.
You can also call .databases when you enter the command prompt.
Or:
sqlite3 test.db ".databases"
Just create an empty file.
> test.db
Symbol ">" here means redirection.
Creating a blank db like this ( provided by mosty-mostacho ) has an advantage of being recognised as a valid SQLite db; better than an empty file.
$sqlite3 foo.db "create table t(f int); drop table t;"
Verify it by :
$file foo.db
foo.db: SQLite 3.x database, last written using SQLite version 3024000
from https://sqlite.org/cli.html
Start the sqlite3 program by typing "sqlite3" at the command prompt,
optionally followed by the name the file that holds the SQLite
database (or ZIP archive). If the named file does not exist, a new
database file with the given name will be created automatically. If no
database file is specified on the command-line, a temporary database
is created and automatically deleted when the "sqlite3" program exits.
my emphasis
I used "drop table" sql command to delete a table from my database.
Strangely, the size of the db file does not change since then, even though I have been adding and deleting data from it.
Does that mean the dropped table is never erased, and if new table is created the new table just overwrite wherever the dropped table was located?
I am using windows 7 64 bits and sqlite3, if relevant.
Yes, SQLite marks the space in the file as free and reuses it as needed.
You can execute VACUUM command to shrink the file (this may be slow).
I have a small statistics program, which you can point to a CSV file. It tries to determine certain properties (like i.E. which columns might be a date). Lately I have been reading a lot about SQLite and would like to port my application to make us of it, as this would make it easier to create new statitics as only a new select would have to be written.
Now what I would like to know is, I know that SQLite can operate in memory, but of course I don't want to always load the whole file into memory as this can become rather big. So I would like to point SQLite to the CSV file and provide the column information, so that I can do queries on it. It would also be cool if I could create an index in memory (or a temprorary directory) so that the statistics will run faster. This would not need to modify the CSV, only do selects.
Can this be done out of the box? If not, can I write my own filemanager and connect it to SQLite, to achieve this? Writing my own filemanager would only be an option if the effort is not to big, as I don't want to write a full blown database code.
SQLite supports reading from a file:
$ cat data.csv
Cheese,7,12.3
Bacon,8,19.4
Eggs,3,20.3
# With no filename SQLite creates the database in memory.
$ sqlite3
sqlite> create table data (name text, units integer, price double);
sqlite> .separator ','
sqlite> .import data.csv data
sqlite> select * from data;
Cheese,7,12.3
Bacon,8,19.4
Eggs,3,20.3
You can add constrains and indexes on this table to help you with your analysis.
In the sqlite3 command line tool I dropped a table:
sqlite> DROP TABLE tableName;
It's a pretty big large table, so I would expect the sqlite file size to drop considerably. However, it hasn't changed at all.
What is the proper way to purge this table from the database completely?
The size of the file does not shrink when you delete a table in SQLite, you have to explicitely ask for it.
More info in the FAQ : http://www.sqlite.org/faq.html#q12
There are also some options to configure your database to have auto-vacuum enabled, you can find more info in the documentation of the VACUUM keyword here : http://www.sqlite.org/lang_vacuum.html and the PRAGMA option here : http://www.sqlite.org/pragma.html#pragma_auto_vacuum
I am running a SQLite database in memory and I am attempting to drop a table with the following command.
DROP TABLE 'testing' ;
But when I execute the SQL statement, I get this error
SQL logic error or missing database
Before I run the "Drop Table" query I check to make sure that the table exists in the database with this query. So I am pretty sure that the table exists and I have a connection to the database.
SELECT count(*) FROM sqlite_master WHERE type='table' and name='testing';
This database is loaded in to memory from a file database and after I attempt to drop this table the database is saved from memory to the file system. I can then use a third party SQLite utility to view the SQLite file and check to see if the "testing" exists, it does. Using the same 3rd party SQLite utility I am able to run the "Drop TABLE" SQL statement with out error.
I am able to create/update tables without any problems.
My questions:
Is there a difference between a memory database and a file database in SQLite when dropping a table?
Is there a way to disable the ability to drop a table in SQLite that I may have accentually turned on somehow?
Edit: It appears to have something to do with a locked table. Still investigating.
You should not have quotes in your DROP TABLE command. Use this instead:
DROP TABLE testing
I had the same problem when using Sqlite with the xerial jbdc driver in the version 3.7.2. and JRE7
I first listed all the tables with the select command as follows:
SELECT name FROM sqlite_master WHERE type='table'
And then tried to delete a table like this:
DROP TABLE IF EXISTS TableName
I was working on a database stored on the file system and so it seems not to effect the outcome.
I used the IF EXISTS command to avoid listing all the table from the master table first, but I needed the complete table list anyway.
For me the solution was simply to change the order of the SELECT and DROP.