How to Use SQLite

Source: Internet
Author: User

Some time ago, someone in the interest group asked,"AndroidUpperSQLite"What is the best practice"? I searched it with curiosity and did not have a good guidance document. I usually just copied itCode. Here is what I seeKevinThe general meaning of the original article is as follows:

AndroidThe example covers someSQLiteBut they do not provide in-depth rational use methods. More importantly, they are not reasonable. Most examples and documents only involve the most basic database queries, or teach you how to createContentprovider. The areas that are never mentioned are as follows:

·Where to create and saveSqliteopenhelperInstance?

·How many instances can be created?

·Is there anything to worry about accessing the database with multiple threads?

The basic content is that you can connectSQLiteDatabase, andAndroidThe system also supports you.SQLiteHas a file-Level Lock for Synchronous access and error prevention. If you only know this, it will bring you great pain. One advantage of open source is that you can go deepCodeTo find out. From the Code and some tests, I learned the following facts:

·SQLiteHas a file-Level Lock. Many threads can read data at the same time, but only one thread can write data. The lock prevents multiple concurrent writes.

·AndroidInSqlitedatabaseSomeJavaLock to ensure that the action is synchronized.

·If you use multiple threads to access the database, your database will not (or should not) Crash.

It is not mentioned that if you write a database through multiple different real connections, one of them will fail, and it will not continue writing after the previous one is completed. In a simple way, you won't write your changes. Even worse, you won't get an exception, just inLogcatOutput someMessage.

SqliteopenhelperClass has done something interesting. Although it can obtain a read-only connection and a read-write connection, they are essentially the same connection. If there is no file write error, the read-only connection is essentially a read/write connection. Interesting. Therefore, if yourAppUseHelperYou have never used multiple connections Even when using multiple threads.

Similarly,HelperOnly oneSqlitedatabaseInstance, which implements someJavaLock. Therefore, when you are performing database operationsDBWill be locked. Even if you use multiple threads to do these tasks to optimize database performance, bad messages are useless.

According to my understanding,SQLiteBasically, it is impossible to destroy your database unless the Code containsBugOr there is a hardware problem.

Therefore, we recommend that you createSqliteopenhelperStatic object. When to goCloseWhat about it? No. WhenAppIf it is disabled, the file reference is automatically released.

However, will there be"Close () was never explicitly called on Database"Exception?

If you note that when the connection is hung there, you do not get the exception. You only encounter exceptions when the connection has been established and you try to open another one. Therefore, you only need to open the connection once.

Use it like this:

Public class databasehelper extends ormlitesqliteopenhelper

{

Private Static databasehelper instance;

 

Public static synchronized databasehelper gethelper (context)

{

If (instance = NULL)

Instance = new databasehelper (context );

 

Return instance;

}

// Other stuff...

}

 

That's all...

Original article address:

Http://www.touchlab.co/blog/android-sqlite-locking/

Http://www.touchlab.co/blog/single-sqlite-connection/

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.