How to solve the problem that database memory is not changed after SQLite data table/record is deleted in C #

Source: Internet
Author: User
Tags sqlite sqlite database

When you use the SQLite database in C #, you find that when you delete a data table or a large amount of data, the database does not become smaller.

This is because when a large amount of data is deleted in the database, the original data space (the free data page) is left, and the database does not automatically release the space by default. You can free up space by using the VACUUM command.

Two methods are found online (manual/Automatic):

1. Free space Manually

Write a function that executes the SQL statement first:

1 Private voidExecuteSQL (stringsDBPath,stringsqlstr)2 {3     using(Sqliteconnection conn =NewSqliteconnection ("Data Source ="+sDBPath))4     {5         using(Sqlitecommand cmd =NewSqlitecommand ())6         {7Cmd. Connection =Conn;8 9 Conn. Open ();TenCmd.commandtext =sqlstr; One cmd. ExecuteNonQuery (); A  - Conn. Close (); -         } the     } -}

After deleting the data table/large amount of data, call the above function (DBPath is the address of the database).

" VACUUM ");

2. Set up the database to automatically free up space

When there is no data table in the database, set its properties:

" PRAGMA auto_vacuum = 1; ");

Testing the results of the discovery is also very good.

Compare two methods, automatic more convenient. However, it is important to note that the database generates a large amount of memory fragmentation when frequent inserts, updates, and deletions occur. Freeing up space automatically frees up free data pages, but does not defragment memory fragments, which in turn generates additional memory fragmentation, while manually freeing free space and defragmenting memory fragments.

How to solve the problem that database memory is not changed after SQLite data table/record is deleted in 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.