Detailed explanation of the operation flow of the database in Android

Source: Internet
Author: User

How to operate a database in Android:

1. The Android platform provides a database helper class to create or open a database. This helper class inherits from the Sqliteopenhelper class. The main task of inheriting and extending the Sqliteopenhelper class is to rewrite the following two methods.
(1). OnCreate (Sqlitedatabase db):
A. The method is executed when the database is first created (when the user first uses the software). This method is not called once the database exists.
B. This function is not called when a helper class (inheriting the Sqliteopenhelper Class) object is generated, only if the getreadabledatabase or getwritabledatabase of the helper class object is called.
C. If the database is created for the first time, the method must be called.
D. Initialization operations such as creating tables are generally performed in this method.
(2). Onupgrade (sqlitedatabse dv, int oldversion,int new Version):
A. This method is called when the database is opened with a different version number than the current version number.
B. Update the database table structure when upgrading the software.
By Implementing these two methods, you can use its getwritabledatabase () and getreadabledatabase () to obtain the database (Sqlitedatabase object).

2. Emphasize that:
(1). When a database is first created, the system automatically calls the OnCreate () method (which can also be called in a program) when implementing the Getreadabledatabase of the helper class object or when Getwritabledatabase is called.
(2). If the user needs to upgrade the database table structure, you need to call Onupgrade (sqlitedatabase db,int oldversion,int vewversion) actively.
method: Pass in the number of a new version.
(3). Inherit Sqliteopenhelper and overwrite OnCreate (), Onupgrade (), OnOpen () (optional). We can create or open a database and upgrade it. Get the Sqlitedatabase instance through Getreadabledatabase and Getwritabledatabase.
(4). Close the database through close.
3. Summary:
Database Operation Flow:
(1). Obtain the Database object first:
Methods for obtaining database objects: The Android platform provides a database helper class that inherits from the Sqliteopenhelper class. Implement OnCreate (), Onupgrade () two methods. Create the Auxiliary class object, and then call the Getwritabledatabase () and Getreadabledatabase () methods to obtain the database object.
(2). Call database additions and deletions to change the method:
Add, delete, modify, and find data to a database table.
(3). Close the database.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Detailed explanation of the operation flow of the database in Android

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.