Android SQLite Database Study Notes

Source: Internet
Author: User
Tags sqlite database

~~~~~~~~~~ SDK contentprovider learning notes ~~~~~~~~~~~

 

The role of contentprovider is as follows:

 

Content Providers store and retrieve data and make it accessible to all applications. They're the only way to share data processing SS applications; there's no common storage area that all android packages can access.

 

 

------- If you want to customize a content provider, you 'd better define a constant for its URI to facilitate client code and subsequent updates. Each content provider opens a public uri (encapsulated by a URI object), which uniquely identifies the content provider dataset. The Uris of all content providers start with the string "content.

Uri constants are used in all interactions with content providers.

 

------- Query content provider:

To query a content provider, you need the following information:

(1) URI that identifies the provider

(2) Names of the data domains to be accepted

(3) Data Types of these data domains

To search for a specific record, you also need to know the ID of the record.

 

Making a query:

You can use the contentresolver. Query () or activity. managerdquery () method.

The first parameter of the preceding two query functions is the URI of the content provider.

If you want to restrict the query to a specific record, you can add the _ id value of the record after the URI. You can useContentUris.withAppendedId()AndUri. withappendedpath () to implement

The returned cursor object can only be used for data reading. The addition, deletion, and modification of data must be implemented through the contentsolver object.

 

Reading retrieved data:

The cursor object provides some methods for reading the queried records, such as getstring ()/getint ()/getfloat (). However, the parameters of these methods are the index of the corresponding column, therefore, you must first obtain the corresponding index using the column name to read data. The cursor object itself supports obtaining the index from the column name and the column name from the index. For example, cursor. getcolumnindex ().

 

modifying data:

The data saved by contentprovider can be modified in the following aspects:

(1) adding new records

(2) adding new values to existing records

(3) batch updating existing records

(4) deleting records

All data modifications must be implemented through the contentresolver method. At the same time, you must obtain the data modification permission in advance, otherwise the modification will fail.

(1) adding records: Use the contentvalues object and contentresolver. insert () method.

If you want to store large multimedia information such as images and sounds in contentprovider, they are generally temporarily stored in the provider, and opened through contentresolver. openoutputstream () method.

(2) adding new values: Use the URI returned by contentresolver. insert () for further processing.

(3) batch updating records: UseContentResolver.update()Method to update the column value.

(4) deleting a record: UseContentResolver.delete()

 

Note: The private data of applications in the android virtual machine is generally stored under/data/{package name}/databases. For example, in the/data/COM. Android. providers. Contacts/databases/directory, the contacts2.db SQLite database file exists.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~

How to Create contentprovider?

It mainly includes the following aspects:

(1) set up a system for storing the data.

Note: Android providesSQLiteOpenHelperClass to help you create a database andSQLiteDatabaseTo manage it.

 

(2) ExtendContentProviderClass to provide access to the data.

 

Note: six member methods must be implemented.

 

Query ()
insert()
update()
delete()
getType()
Oncreate (): called when the provider is started.

 

These methods may be called by the contentresolver object.

call ContentResolver.notifyChange() to notify listeners when there are modifications to the data.

 

 

Several steps for defining contentprovider:

----Define a public static final Uri named CONTENT_URI. This is the string that represents the full content: URI that your content provider handles.  

-------- Define the column names that the content provider will return to clients.

-------- Carefully document the Data Type of each column. clients need this information to read the data.

-------- If you are handling a new data type, you must define a new MIME type to return in your implementationContentProvider.getType().

 

(3) Declare the content provider in the manifest file for your application (androidmanifest. XML ).

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ··

 

Summary:

 

Tip: For custom contentprovider, some static data may be used by other applications, so it can be exported as a jar archive file.

 

When developing Android applications, be sure to add proper access permissions to the androidmanifext. xml file. Otherwise, an inexplicable error message may occur.

 

After reading the content in contentprovider In the android SDK:

The relationship between cursor, contentprovider, and contentresolver should be as follows:

Contentprovider is mainly responsible for data storage and underlying data maintenance operations. It does not directly communicate with customer code.

Contentresolver is mainly responsible for client code and directly responds to operations on the UI interface. It calls some callback functions in contentprovider to add, delete, modify, and query data.

Cursor is mainly used to display data obtained from contentprovider. It can only display and read data and cannot be modified.

 

 

Data maintenance operations in SQLite are generally obtained through the inheritance subclass of sqliteopenhelper to obtain the sqlitedatabase object instance, and then implement data operations, which is more convenient.

In actual programming and development, the operation of cursor's subclass object is generally not directly used to display data. Instead, the cursor subclass object is linked with the adapter space to display data.

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.