1. SQLite
android.database.sqlite
Contains the SQLite database management classes that an application would use to manage its own private database. Applications use these classes to manage private databases. If creating a content provider, you will probably have to use these classes to create and manage your own database to store content. See Content Providers to learn the conventions for implementing a content provider. If you are working with data sent to you by a provider, you do not use these SQLite classes, but instead use the generic android.database
classes.
The Android SDK and Android emulators both include the sqlite3 command-line database tool. On your development machine, run the tool from theplatform-tools/
folder of your SDK. On the emulator, run the tool with adb shell, for example, adb -e shell sqlite3
.
Some device manufacturers include different versions of SQLite on their devices. There are two ways to programmatically determine the version number.
- If available, use the sqlite3 tool, for example:
adb -e shell sqlite3 --version
. - Create and query an in-memory database as shown in the following code sample:
String query = "select sqlite_version() AS sqlite_version";
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(":memory:", null);
Cursor cursor = db.rawQuery(query, null);
String sqliteVersion = "";
if (cursor.moveToNext()) {
sqliteVersion = cursor.getString(0);
}
2.ContentProvider
Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver
interface. A content provider is only required if you need to share data between multiple applications. For example, the contacts data is used by multiple applications and must be stored in a content provider. If you don't need to share data amongst multiple applications you can use a database directly via SQLiteDatabase
.
When a request is made via a ContentResolver
the system inspects the authority of the given URI and passes the request to the content provider registered with the authority. The content provider can interpret the rest of the URI however it wants. The UriMatcher
class is helpful for parsing URIs.
The primary methods that need to be implemented are:
onCreate()
which is called to initialize the providerquery(Uri, String[], String, String[], String)
which returns data to the callerinsert(Uri, ContentValues)
which inserts new data into the content providerupdate(Uri, ContentValues, String, String[])
which updates existing data in the content providerdelete(Uri, String, String[])
which deletes data from the content providergetType(Uri)
which returns the MIME type of data in the content provider
Data access methods (such as insert(Uri, ContentValues)
and update(Uri, ContentValues, String, String[])
) may be called from many threads at once, and must be thread-safe. Other methods (such as onCreate()
) are only called from the application main thread, and must avoid performing lengthy operations. See the method descriptions for their expected thread behavior.
Requests to ContentResolver
are automatically forwarded to the appropriate ContentProvider instance, so subclasses don't have to worry about the details of cross-process calls.