Feelings about using SQLite

Source: Internet
Author: User

I have been using the SQLite database recently. To be honest, this is the third database I have used. The reason for using it should be that it is lightweight and has good performance. However, some problems have also been found after a long time of use. Occasionally, there will also be complaints, but I will not scold SQLite for being a broken thing or something. There are several reasons: I have a thorough understanding of SQLite, and I am not familiar with the use of SQLite, the ability to solve the problem is limited; I am not capable of writing a database better than SQLite.

I checked the applicable scenarios of SQLite on the Internet. Websites with 100000 visits/day, embedded devices and application software, and applicationsProgramFile format. However, when the data can be read, the lock table exception will also be thrown. SQLite locks are coarse-grained, so we do not know how to handle the exception. What else is the database corruption. Database disk image is malformed. There are three reasons for database damage on the Internet:

    1. SQLite databases are interrupted due to power outages during write operations.
    2. The disk space in the SQLite database is insufficient.
    3. Disk has bad channels, etc.

The damage to the database was estimated to be the first cause. At that time, no data was backed up. Only the. Dump command can be manually used to export data from the damaged database. Let's talk about this.

In the command mode of SQLite, open the database and enter the following commands in sequence.

 
SQLite>. Mode insertsqlite>. Output test. sqlsqlite>. Dumpsqlite>. Exit

Of course, you do not have to run the ". Exit" command. Press "Ctrl + C" or directly turn it off. The exported file name "test. SQL" is your favorite.

Or it is better to use this command.

Sqlite3.exe dbname. DB. Dump> filename. SQL

The SQL statement exported by this command is more neat than the one above.

No matter which type of exported SQL statement, if there is a problem with the database, there will be this line in the exported SQL statement

 
/** ** Error: (11) database disk image is malformed *****/

Finally, you can only roll back the transaction rollback.

If you want to use the data back, you can change the rollback transaction and delete the error.

Then, import the data.

 
Sqlite3.exe newdbname. dbsqlite>. Read test. sqlsqlite>. Exit

In the future, we plan to regularly back up SQLite. The backup method is simply file I/O operations-copy a copy. If a database disk image is malformed exception is thrown during database reading and writing, the database is deemed to have been damaged and immediately changed to the backup database.

When writing data to a database and operating multiple records in the database, you can use a transaction package to perform data operations on multiple tables. When writing data to the database, A file named in the format of database file name + "-Journal" is generated under the same directory as the database file. This file will be deleted immediately after the transaction is submitted or rolled back. However, I heard that this file is not deleted in Android, but it only exists in a 0-B size file. When a program is started, if the current host is down due to a power failure and the rollback log file is saved on the disk, SQLite will find the temporary file in the process of opening the database file. SQLite will restore the database based on the file before it opens the database successfully, to ensure that the database data is returned to the status before the previous transaction starts. In this way, database corruption cannot be avoided, but data status consistency can be ensured.

As you have a low level of experience and understanding, please also point out that if you have any good opportunities, please kindly advise. Thank you!

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.