sqlite: does dropping a database table delete the data? - sqlite

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).

Related

copy sqlite index from one database to another

I have a massive database (~800 GB) with several indexed tables. I need to copy one table (including indexes) to a new database. Copying the table itself is pretty straightforward.
$ sqlite3 newDB
> attach database 'oldDB.db' as oldDB
> create table newTable as select * from oldDB.oldTable
But I can't seem to find any information on a way to also copy over an index. Is there any way to do this? Since the tables are so large I'd really like to avoid having to re-index them.
SQLite has no mechanism to copy index contents.
If this particular table would be the majority of the data in the database, the fastest way to copy it would be to copy the database file and then to drop all other tables.
But otherwise, there you cannot avoid the reindex operation.
Please note that CREATE TABLE ... AS ... does copy only the contents of the table, but not the complete table definition (such as column types or constraints).
Copying large table in a single transaction is not a good idea. If you really have to you should turn off journaling first (destination database):
PRAGMA journal_mode=OFF;
As the others have stated, the index cannot be broken out. I suspect that time spent copying the database and then dropping a very large table would be longer than just -> 1. creating the new destination database, 2. determining the original CREATE TABLE statement (from the SQLITE_MASTER table of the source database) and recreating the table in the destination database. Then 3. just ATTACH your destination database to the source database and INSERT INTO destinationdb.tablename SELECT * FROM sourcedb.tablename;* to get the copy rolling.

SQLite dropped table not deleted

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

Drop Table doesnot result on deleting data in the sqlite file

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;'

Temp tables are created by default in sqlite

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.

Sqlite: How do I reset all database tables?

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)

Resources