Databases

The Android API contains support for creating and using SQLite databases. Each database is private to the application that creates it.

The SQLiteDatabase object represents a database and has methods for interacting with it — making queries and managing the data. To create the database, call SQLiteDatabase.create() and also subclass SQLiteOpenHelper.

As part of its support for the SQLite database system, Android exposes database management functions that let you store complex collections of data wrapped into useful objects. For example, Android defines a data type for contact information; it consists of many fields including a first and last name (strings), an address and phone numbers falso strings), a photo (bitmap image), and much other information describing a person.

Android ships with the sqlite3 database tool, which enables you to browse table contents, run SQL commands, and perform other useful functions on SQLite databases. See Examine databases (sqlite3) to learn how to run this program.

All databases, SQLite and others, are stored on the device in /data/data/package name/databases.

Discussion of how many tables to create, what fields they contain, and how they are linked, is beyond the scope of this note, but Android does not impose any limitations beyond the standard SQLite concepts. We do recommend including an autoincrement value key field that can be used as a unique ID to quickly find a record. This is not required for private data, but if you implement a content provider, you must include such a unique ID field. See the Content Providers document for more information on this field and the NotePadProvider class in the NotePad sample code for an example of creating and populating a new database. Any databases you create will be accessible by name to any other class in the application, but not outside the application.

0 0

Post a comment