Use of Android--sqlite

Source: Internet
Author: User

1, familiar with the general use of SQLite, in the actual development, in order to better manage and maintain the database, we will encapsulate a inherit from the Sqliteopenhelper class database operation class, and then based on this class, and then encapsulate our business logic method. For example: We create a DBHelper class that inherits Sqliteopenhelper as the base class for maintaining and managing databases, and then creates a Dbmanager class that is built on top of DBHelper, encapsulating a series of business methods (CRUD, etc.).

2, Getwritabledatabase () and Getreadabledatabase () the difference:

--getreadabledatabase (): From the source code can be seen, the method first to determine whether the database instance is already present and open state, if so, then directly return the instance, otherwise try to get a read-write mode of the database instance, That is, call the Getwritabledatabase () method, if you encounter a situation where disk space is full, and then get a failure, open the database in read-only mode, get the DB instance and return, and assign the Mdatabase to the newly opened DB instance.

--getwritabledatabase (): First Judge Mdatabase if not open is not read-only mode is returned directly, otherwise if mdatabase is not empty lock, and then start to open or create a database, compare versions, based on the version number to invoke the appropriate method, Set a new version number for the database, finally release the old non-empty mdatabase and unlock it, assign the newly opened DB instance to Mdatabase, and return the latest instance.

--Summary: Getreadabledatabase () generally returns the same DB instance as Getwritabledatabase (), so getwritabledatabase () is generally called to get the instance, and when an exception occurs, The Getreadabledatabase () is called again, but the instance at this time is read-only.

3. Cursor management, Activity provides the startmanagingcursor (cursor cursor) method, which manages the current cursor object based on the life cycle of the activity:

The--startmanagingcursor () method manages the life cycle of the current cursor object based on the activity's lifecycle, meaning that when activity stops, he automatically calls the cursor's Deactivate method, disables the cursor, When activity returns to the screen, it calls the cursor's Requery method to query again, and when the activity is destroyed, the managed cursor is automatically closed for release.

--According to SQLite's specification, the primary key is "_id", and the result set must contain the "_id" column when wrapping the cursor.

Use of Android--sqlite

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.