Android data storage-available storage solution (3)

Source: Internet
Author: User
Tags sqlite query

Use Database

Android provides full support for SQLite databases. Any database you create in the application, and any class in the application can be accessed by name. We recommend that you create an SQLite database by creating a subclass of the sqliteopenhelper class and override the oncreate () method. In this method, you can run the SQLite command to create tables in the database.

For example:

Public class dictionaryopenhelper extends sqliteopenhelper {

 


Private Static final int database_version = 2;


Private Static final string dictionary_table_name = "Dictionary ";


Private Static final string dictionary_table_create =


"Create Table" + dictionary_table_name + "(" +


Key_word + "text," +


Key_definition + "text );";

 


Dictionaryopenhelper (context ){


Super (context, database_name, null, database_version );


}

 


@ Override


Public void oncreate (sqlitedatabase dB ){


Db.exe csql (dictionary_table_create );


}

}

You can use the constructor defined in the sqliteopenhelper class implementation to obtain an instance. To read and write data from a database, call the getwritabledatabase () and getreadabledatabase () methods respectively. Both methods return a sqlitedatabase object representing the database and provide the SQLite operation method.

You can use sqlitedatabase. the query () method is used to execute SQLite queries. This method receives various query parameters, such as the tables to be queried, query conditions, selected columns, groups, and mappings among others. For complex queries, such as columns with aliases, you should use the sqlitequerybuilder class, which provides several convenient query methods.

Each SQLite query returns a cursor object pointing to all queried rows. You can use this cursor mechanism to view the database query results.

Database debugging

The android SDK contains the sqlite3 database tool, which allows you to browse table content, run SQL commands, and execute useful functions on other SQLite databases. See examining
Sqlite3 databases from a remote shell to learn how to use this tool.

Use Network Connection

You can use the network (when it is available) to save and restore web service-based data. Use the classes in the following package to perform network operations.

1.
Java.net .*

2.
Android.net .*

Tip:Android does not impose any restrictions on the standard SQLite concept. We recommend that you include an auto-increment primary key field. You can use this unique ID to quickly find records. This is not mandatory for private data, but if you want to implement a content provider, you must include the unique ID used by a basecolumns. _ id constant.

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.