One of the four components of Android, contentprovider, is used to share data. Many application providers inherit it, such as mediaprovider.
There are several classes used to operate databases in Android: sqlitedatabase and sqliteopenhelper. Sqlitedatabase is a database class that instantiates and generates database objects. Sqliteopenhelper is often used as an Assistant class. However, sqliteopenhelper is an abstract class. It must be inherited and then instantiated during use. It generally uses databasehelper to inherit the callback functions of sqliteopenhelper and override parent classes.
Sqliteopenhelper is an Assistant class used to create databases and manage versions. There are three Abstract METHODS: oncreate (), onupdate, and onopen, which are called callback functions automatically in some cases, the subclass override is required. For example, oncreate is called during creation. Call getreadabledatabase () and getwriteabledatabase () to obtain a sqlitedatabase, and then add, delete, modify, and query the database.
Sqlitedatabase is a class used to manage external methods of a database. Mainly include creating, deleting, and executing SQL commands, such as close () to close the database, delete a row of the database, execsql to execute SL commands, insert into a row...
Databasehelper class, constructor is very important.
Create a database: new databases (). At this time, oncreate is not called, And a sqlitedatabase object is returned only after getreadabledatabase (), that is, oncreate is called. Place the created database object under/data/and run the sqlite3 database_name command to enter the database operation mode. You can enter a database statement to perform the operation.
When the version information of the constructor changes, onupgrade is automatically called for upgrade.
Contentvalue is often used for database write operations,
Updating a database is equivalent to executing an update statement. Database_name.update (table_name, contentvalue_object, where)
Query. The cursor is used. When Using cursor, the first is to constantly move the cursor. movetonext (), and the second is to judge whether the next record is null (if (cursor = NULL )). To retrieve the data of a column in a record, two steps are required: cursor. getstring (INDEX), and the index is obtained through getcolumnindex.
Summary: databases are your own weaknesses. When using multimedia, you will always encounter such database and mediaprovider problems, so pay attention to them.
On the csdn post, I saw such a refined explanation:
What is sqlitedatabase?
An instance of sqlitedatabase represents an SQLite database. Through some methods of the sqlitedatabase instance, we can execute SQL statements to add, delete, query, and modify databases. Note that the database is private to an application, and the database name is unique in an application.
Content Provider:
Usually through contentresolver. You can use getcontentresolver () to obtain a contentresolver from the implementation of an activity or other application component:
Querying a content provider
You need three pieces of information to query a content provider:
• URI used to identify the content provider
• Name of the data field you want to obtain
• Data Types of these fields
Generate a query making the query
You can use the contentresolver. Query () method or the activity. managedquery () method to query a content provider. Both methods use the same parameter sequence and return a cursor object.
Batch update record batch updating records
To update a group of records in batches (for example, change "NY" in all fields to "New York"), you can call contentresolver by passing the column and value parameters to be changed. update () method.
Delete a record deleting a record
To delete a single record, you can use the URI parameter of a Specific Row to call the contentresolver. Delete () method.
Create a content provider creating a content provider
To create a content provider, you must:
• Create a data storage system. Most content providers use the android file storage method or SQLite database to store their data, but you can store data in any way you want. Android provides the sqliteopenhelper class to help you create a database and manage it using the sqlitedatabase class.
• Extended contentprovider class to provide data access interfaces.