How Android data is stored

Source: Internet
Author: User
Tags sqlite database

Data storage is most frequently used in development, and there are 5 main ways to implement data storage in the Android platform, namely:

1 Sqlite:sqlite is a lightweight embedded database that supports basic SQL syntax and is often used as a data storage method. Android provides a class named Sqlitedatabase for this database, encapsulating some of the APIs that manipulate the database.

2 sharedpreference:sharedpreferences is also a lightweight way of storing data, essentially storing key-value key-value pairs of data based on XML files , typically used to store some simple configuration information. Its storage location is under the/data/data/< package name >/shared_prefs directory. The Sharedpreferences object itself can only read data and does not support writing data, and storage modifications are implemented through the editor object. often used to store simpler parameter settings.

3 file: The commonly used method of storing files (I/O), often storing large amounts of data, but the disadvantage is that updating the data will be a difficult task.

4 Contentprovider:android system can realize all the application sharing of a data storage method, because the data is usually in each application between the private, so this storage method is less used, but it is an indispensable way of storage. For example, audio, video, pictures, and contacts can generally be stored in this way. Each content provider provides a public URI (wrapped as a URI object), and if the application has data to share, it needs to use content provider to define a URI for the data, and then the other application passes the content Provider pass in the URI to manipulate the data.

5 Networked Storage data: Get data from the network and write data to the network storage space

SQLite mainly through two classes to realize its function

Sqliteopenhelper: This class is a helper class for databases, for database creation, for table creation, and for version updates

Sqlitedatabase: This class encapsulates the method of specific operation of the table, such as adding and deleting the method.

sharedpreferences with the SQLite the difference between databases

Compared with SQLite database, Sharedpreferences object is more convenient and concise than creating database, creating table, writing SQL statement and so on. But Sharedpreferences also has its own flaws, such as its functional storage boolean,int,float,long and string five of simple data types, such as its inability to perform conditional queries. So no matter how simple the sharedpreferences data store operation is, it can only be a supplement to the storage method, and it cannot completely replace other data storage methods such as SQLite database.

ContentProvider and SQLite's difference

SQL can only share data within the project, and ContentProvider can share data between projects.

contentprovider URI Component

A, the standard prefix: ContentProvider (content provider) scheme has been provided by Android, scheme is: content://

B, uniquely identifies the entire content Provider: the hostname (or authority) is used to uniquely identify the contentprovider, and the external caller can find it based on that identity.

C, describes the path to the data, determines what kind of data is returned: path can be used to represent the data we want to manipulate, the path should be built according to the business

ID unique Callout Request data: The ID of the data we want to request

get through Contentresolver ContentProvider basic steps for content

First: Get Contentresolver class object: Contentresolver CR = Getcontentresolver ();

Second: Defines the field string array to query.

Third: Use Cr.query (); Returns a Cursor object.

IV: Use the while loop to get the contents of the cursor.

Steps to customize ContentProvider

1. inherit Android's ContentProvider base class to implement its own ContentProvider class.

2. Implement the OnCreate method in ContentProvider, create provider data, usually database SQLite.

3. rewrite the corresponding insert/delete/update/query additions and deletions in contentprovider to change these methods.

4. Register the ContentProvider class in the Androidmanifest.xml file and specify the URI for ContentProvider. The URI is similar to the URL address used in Web Access.

Configure <provider/> tags and properties in <application></application>: Android:name property: ContentProvider class name

Android:authorities property: The name used to access the ContentProvider.

access to custom ContentProvider in Android

Access to open data through the URI of the ContentProvider.

(1) The Contenresolver object is obtained through the method Getcontenresolver () provided by the context.

(2) Contenresolver provides the following methods to operate: Insert Delete Update query these methods call the corresponding method in Contenprovider and get the result returned.

Http://wenku.baidu.com/link?url=v6y-2Lk_ Tzy56zpdq8i3er8vxno0jnb6tl81-xpkhsuwgz7opf3knbfvgpogmoga3wwccybwgwqlzc5lo5ko3moh7jsvymwaadhwhliddf3

Http://www.360doc.com/content/16/0319/00/31777725_543463009.shtml

How Android data is stored

Related Article

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.