Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Method for Database CleanUp in SQlite?

Tags:

c#

sqlite

ado.net

As what i experience using Sqlite for my Small Applications i always use sqliteadmin to use its database cleanup function to removes unnecessary data on my database.

Now i want to create a Method in my Application which do the same way as sqliteadmin CleanUp.

How to do this?

Thanks in Regards

like image 916
BizApps Avatar asked Jun 01 '11 09:06

BizApps


People also ask

How do I clear data in SQLite?

sqlite> DELETE FROM table_name; Following is the basic syntax of DROP TABLE. sqlite> DROP TABLE table_name; If you are using DELETE TABLE command to delete all the records, it is recommended to use VACUUM command to clear unused space.

What does SQLite VACUUM do?

The VACUUM command works by copying the contents of the database into a temporary database file and then overwriting the original with the contents of the temporary file. When overwriting the original, a rollback journal or write-ahead log WAL file is used just as it would be for any other database transaction.

What does the VACUUM command do?

A VACUUM DELETE reclaims disk space occupied by rows that were marked for deletion by previous UPDATE and DELETE operations, and compacts the table to free up the consumed space.

Can I delete SQLite files?

SQLite stores its databases as a normal file within the computer's file system, so creating and dropping databases is not really applicable. If you need to completely remove a database, you will need to delete the database file from the file system. Then you can navigate to the file in the file system and delete it.


3 Answers

using (SQLiteCommand command = m_connection.CreateCommand())
{
    command.CommandText = "vacuum;";
    command.ExecuteNonQuery();
} 

here is the exact answer on how to execute vacuum.

like image 105
BizApps Avatar answered Sep 20 '22 08:09

BizApps


It seems you're looking for the VACUUM statement.

like image 36
Frédéric Hamidi Avatar answered Sep 21 '22 08:09

Frédéric Hamidi


Interesting post script. The Vacuum statement in SQLite copies the entire database to a temp file for rebuilding. If you plan on doing this "On Demand" via user or some process, it can take a considerable amount of disk space and time to complete once your database gets above 100MB, especially if you are looking at several GB. In that case, you are better off using the AUTO_VACUUM=true pragma statement when you create the database, and just deleting records instead of running the VACUUM. So far, this is the only advantage I can find that SQL Server Compact has over SQLite. On demand SHRINK of the Sql Server Compact database is extremely fast compared to SQLite's vacuum.

like image 34
David C Avatar answered Sep 22 '22 08:09

David C