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
Related
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 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;'
I have created a database "MyDB.sqlite" using the command line sqlite3 MyDB.sqlite in a specific folder(my desktop) and then created a table "tbl11" using create table syntax. I am able to insert record and can check the inserted records.
But when I exit command (terminal in Mac) line and re enter I can't see my database and tables in that folder. I guess this database and table are temporary by default. I even check the .databases command to see the database, but I can only see two database main!
please help!
Be sure to finish off with COMMIT; :-)
For practice, work through the short working example at http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html
Good luck.
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.
I want a debug function to do this, but I'm unaware of whether one already exists. Going through and using 'drop table' for each of my tables will be a pain.
Help appreciated.
Since the database is just one file, you can indeed just erase it. If you want something more automatic, you can use the following to do it all programmatically:
Recover your schema:
SELECT group_concat(sql,';') FROM sqlite_master;
Disconnect from the database
Delete the database file
Create your schema again with what was returned from the above query
If you used any particular options for your original database (page_size, etc), they will have to be declared manually as well.
to "drop database" for sqlite, simply delete the database file (and recreate if needed)