How to store your Android sqlite data

Source: Internet
Author: User

Sqliteopenhelper class

Use the SQLiteOpenHelper and methods in the class getWritableDatabase() getReadableDatabase() to get a reference to the database.

To enable the management of database versions, the SQLiteOpenHelper class provides two important methods, namely onCreate() onUpgrade() , the first to generate a database table when using the software, and the database table structure to be updated when the software is upgraded.

When calling Sqliteopenhelper's Getwritabledatabase () or Getreadabledatabase () method to get the Sqlitedatabase instance used to manipulate the database, if the database does not exist, The Android system automatically generates a database and then calls the OnCreate () method.

 The onCreate ()  method is called only when the database is first generated, and in the OnCreate () method, the database table structure can be generated and the initialization data used by some applications to be added.

  The Onupgrade () method is called when the version of the database changes, and the version number is usually changed when the software is upgraded, and the version of the database is controlled by the programmer.

Assuming that the database version is 1, due to business changes, modify the database table structure, it is necessary to upgrade the software, upgrade the software to update the database table structure of the user's phone, in order to achieve this goal, the original database version can be set to 2, and in Onupgrade () method to implement the table structure update.

When the version of the software upgrade more than the number, then in the Onupgrade () method can be based on the original number and the target version number to judge, and then make the corresponding table structure and data update.

Sqlitedatabase class

Android provides a SQLiteDatabase class named ( SQLiteOpenHelper and methods in the class that getWritableDatabase() getReadableDatabase() return objects of this class).

  SQLiteDatabaseThe class encapsulates some APIs that manipulate the database, which you can use to complete the Add (Create), query (Retrieve), update, and delete operations of the data (these operations are referred to as CRUD).

Sqlitedatabase's study should focus on mastering the Execsql () and Rawquery () methods.

  The Execsql () method can perform SQL statements with change behavior such as INSERT, delete, update, and create table;

  The rawquery () method is used to execute a SELECT statement.

How to store your Android sqlite data

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.