This article translated from: http://developer.android.com/training/basics/data-storage/databases.html
It is a good idea to store duplicate or structured data (such as communication records) in a database. This course assumes that you are familiar with the usual SQL database and help you start using the SQLite database on the Android platform. On the Android platform, the database APIs you need are included in the Android. database. SQLite package.
Define patterns and constraints
One of the main principles of SQL data is the mode: a formal statement on how the database is organized. The mode is reflected in the SQL statement you use to create a database. You may find that it helps to create a contract class, which uses systematic and self-recorded methods to explicitly specify the layout of your mode.
The contract class is a constant container that defines the Uris, table name, and column name. All classes in the same package can use constants in this constraint class. In this way, a change is made globally.
A good way to organize a constraint class is to put the definition at the root level of the class so that it is effective for the entire database.
Note: by implementing the basecolumns interface, your internal class will inherit a primary key named _ ID, and some Android classes (such as the cursor adapter) will want this field. It is not necessary, but this field will help your database and Android framework work more harmoniously.
For example, the following code snippet defines the table name and column name of a table:
Publicstatic1_actclassfeedentryimplementsbasecolumns {
Public static final string table_name = "entry ";
Public static final string column_name_entry_id = "entryid ";
Public static final string column_name_title = "title ";
Public static final string column_name_subtitle = "subtitle ";
...
}
To prevent the constraint class from being accidentally instantiated, its constructor is private:
// Prevents the feedreadercontract class from being instantiated.
Private feedreadercontract (){}
Use the SQL helper to create a database
Once you define your database, you should create and maintain the database and table. The following are typical statements for creating and deleting tables:
Privatestaticfinalstring
Text_type = "text ";
Private Static final string comma_sep = ",";
Private Static final string SQL _create_entries =
"Create Table" + feedreadercontract. feedentry. table_name + "(" +
Feedreadercontract. feedentry. _ ID + "integer primary key," +
Feedreadercontract. feedentry. column_name_entry_id + text_type + comma_sep +
Feedreadercontract. feedentry. column_name_title + text_type + comma_sep +
... // Any other options for the CREATE Command
")";
Private Static final string SQL _delete_entries =
"Drop table if exists" + table_name_entries;
Just as you save files in the device's internal memory, Android will save your database in the private hard disk space associated with the application. By default, this area is inaccessible to other applications, so your data is secure.
It is helpful to use a group of APIs in the sqliteopenhelper class. Because when this class is used to obtain database references, the system only performs long-time operations for potential database creation and updating when necessary and during non-startup of the application. You need to call the getwritabledatabase () or getreadabledatabase () method.
Note:Because they are long-term operations, you must ensure that the getwritabledatabase () and getreadabledatabase () methods are called in the background thread, for example, in asynctask or intentservice.
To use sqliteopenhelper, you must inherit the sqliteopenhelper class to create a subclass, override the oncreate (), onupgrade (), and onopen () callback methods, and selectively implement the ondowngrade () callback method.
For example, the following is a subclass implementation of the sqliteopenhelper class:
Publicclassfeedreaderdbhelperextendssqliteopenhelper {
// If you change the database schema, you must increment the database version.
Public static final int database_version = 1;
Public static final string database_name = "feedreader. DB ";
Public feedreaderdbhelper (context ){
Super (context, database_name, null, database_version );
}
Public void oncreate (sqlitedatabase dB ){
Db.exe csql (SQL _create_entries );
}
Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion ){
// This database is only a cache for online data, so its upgrade policy is
// To simply to discard the data and start over
Db.exe csql (SQL _delete_entries );
Oncreate (db );
}
Public void ondowngrade (sqlitedatabase dB, int oldversion, int newversion ){
Onupgrade (dB, oldversion, newversion );
}
}
Use the following methods to access your database:
Feedreaderdbhelper mdbhelper
= Newfeedreaderdbhelper (getcontext ());