Considerations for compressing SQLite databases using C #

Source: Internet
Author: User
Tags sqlite sqlite database

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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.