When a large amount of data is deleted from the SQLite database, if the database file is not smaller, you can perform a VACUUM command that will reorganize the database and free up extra space.
Test Insert Data
try {sqliteconnection cnn = new Sqliteconnection (@ "Data source=g:\app_d Ata\data.sqlite; "); Cnn. Open (); using (sqlitetransaction Dbtrans = CNN. BeginTransaction ()) {using (Sqlitecommand cmd = CNN). CreateCommand ()) {try { Cmd. CommandText = @ "INSERT into Students (Name, Description) VALUES (' 1 ', ' 2 ')"; for (int n = 0; n < 100000; n++) {cmd. ExecuteNonQuery (); } dbtrans.commit (); } catch {Dbtrans.rollback (); } } } } catch (Exception ex) {Clipboard.settext (ex. Message + "\r\n\r\n" + ex. StackTrace); }
After you delete the data in the table, you find that the database file is not smaller.
Perform the following actions:
using (sqliteconnection connection = new Sqliteconnection (connectionString)) { using (sqlitecommand cmd = new Sqlitecommand ("VACUUM", Connection)) { try { connection. Open (); Cmd. ExecuteNonQuery (); } catch (System.Data.SQLite.SQLiteException E) { connection. Close ();}} }
The discovery database file becomes 2kb.
Note In the Compress database execution operation, do not use the transaction transaction, or it will fail.
Considerations for compressing SQLite databases using C #