ITEEDU

Using SQLite Databases

Android supports a SQLite database system and exposes database management functions that let you store complex collections of data wrapped into useful objects. For example, Android defines a Contact data type that consists of many fields including string first and last names, string address and phone numbers, a bitmap image, and much other information describing the person. To create a database, use SQLiteOpenHelper and read and write this data as appropriate. (Note that file data such as a bitmap is typically stored as a string file path value in the database, with the location of the local file.)

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 document, 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 sample class NotePadProvider.java in the NotePad sample project 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.