Saving data to a database is ideal for repeating or structured data, such as contact information. This page assumes that you are familiar with SQL databases in general and helps you get started with SQLite databases on Android. The APIs you'll need to use a database on Android are available in the Show private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"0 package. Caution: Although these APIs are powerful, they are fairly low-level and require a great deal of time and effort to use:
For these reasons, we highly recommended using the Room Persistence Library as an abstraction layer for accessing information in your app's SQLite databases. Define a schema and contractOne of the main principles of SQL databases is the schema: a formal declaration of how the database is organized. The schema is reflected in the SQL statements that you use to create your database. You may find it helpful to create a companion class, known as a contract class, which explicitly specifies the layout of your schema in a systematic and self-documenting way. A contract class is a container for constants that define names for URIs, tables, and columns. The contract class allows you to use the same constants across all the other classes in the same package. This lets you change a column name in one place and have it propagate throughout your code. A good way to organize a contract class is to put definitions that are global to your whole database in the root level of the class. Then create an inner class for each table. Each inner class enumerates the corresponding table's columns. Note: By implementing the private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"1 interface, your inner class can inherit a primary key field called private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"2 that some Android classes such as private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"3 expect it to have. It's not required, but this can help your database work harmoniously with the Android framework. For example, the following contract defines the table name and column names for a single table representing an RSS feed: Kotlinobject FeedReaderContract { }Javapublic final class FeedReaderContract { }Create a database using an SQL helperOnce you have defined how your database looks, you should implement methods that create and maintain the database and tables. Here are some typical statements that create and delete a table: Kotlinprivate const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"Javaprivate static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
Just like files that you save on the device's , Android stores your database in your app's private folder. Your data is secure, because by default this area is not accessible to other apps or the user.The private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"4 class contains a useful set of APIs for managing your database. When you use this class to obtain references to your database, the system performs the potentially long-running operations of creating and updating the database only when needed and not during app startup. All you need to do is call private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"5 or private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"6. Note: Because they can be long-running, be sure that you call private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"5 or private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"6 in a background thread. See Threading on Android for more information. To use private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"4, create a subclass that overrides the private static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
0 andprivate static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
1 callback methods. You may also want to implement theprivate static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
2 orprivate static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
3 methods, but they are not required.For example, here's an implementation of private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"4 that uses some of the commands shown above: Kotlinclass FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }Javapublic class FeedReaderDbHelper extends SQLiteOpenHelper { }To access your database, instantiate your subclass of private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"4: Kotlinval dbHelper = FeedReaderDbHelper(context) JavaFeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getContext()); Put information into a databaseInsert data into the database by passing a private static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
6 object to the private static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
7 method:Kotlin// Gets the data repository in write mode val db = dbHelper.writableDatabase // Create a new map of values, where column names are the keys val values = ContentValues().apply { }
// Insert the new row, returning the primary key value of the new row
val newRowId = db?.insert(FeedEntry.TABLE_NAME, null, values)Java// Gets the data repository in write mode SQLiteDatabase db = dbHelper.getWritableDatabase(); // Create a new map of values, where column names are the keys ContentValues values = new ContentValues(); values.put(FeedEntry.COLUMN_NAME_TITLE, title); values.put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle); // Insert the new row, returning the primary key value of the new row long newRowId = db.insert(FeedEntry.TABLE_NAME, null, values); The first argument for private static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
7 is simply the table name. The second argument tells the framework what to do in the event that the private static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
6 is empty (i.e., you did notclass FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }0 any values). If you specify the name of a column, the framework inserts a row and sets the value of that column to null. If you specify class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }1, like in this code sample, the framework does not insert a row when there are no values. The private static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
7 methods returns the ID for the newly created row, or it will return -1 if there was an error inserting the data. This can happen if you have a conflict with pre-existing data in the database.Read information from a databaseTo read from a database, use the class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }3 method, passing it your selection criteria and desired columns. The method combines elements of private static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
7 and class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }5, except the column list defines the data you want to fetch (the "projection"), rather than the data to insert. The results of the query are returned to you in a class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }6 object. Kotlinpublic final class FeedReaderContract { }0 Javapublic final class FeedReaderContract { }1 The third and fourth arguments ( class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }7 and class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }
class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }3 reference. To look at a row in the cursor, use one of the class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }6 move methods, which you must always call before you begin reading values. Since the cursor starts at position -1, calling public class FeedReaderDbHelper extends SQLiteOpenHelper { }1 places the "read position" on the first entry in the results and returns whether or not the cursor is already past the last entry in the result set. For each row, you can read a column's value by calling one of the class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }6 get methods, such as public class FeedReaderDbHelper extends SQLiteOpenHelper { }3 or public class FeedReaderDbHelper extends SQLiteOpenHelper { }4. For each of the get methods, you must pass the index position of the column you desire, which you can get by calling public class FeedReaderDbHelper extends SQLiteOpenHelper { }5 or public class FeedReaderDbHelper extends SQLiteOpenHelper { }6. When finished iterating through results, call public class FeedReaderDbHelper extends SQLiteOpenHelper { }7 on the cursor to release its resources. For example, the following shows how to get all the item IDs stored in a cursor and add them to a list: Kotlinpublic final class FeedReaderContract { }2 Javapublic final class FeedReaderContract { }3 Delete information from a databaseTo delete rows from a table, you need to provide selection criteria that identify the rows to the public class FeedReaderDbHelper extends SQLiteOpenHelper { }8 method. The mechanism works the same as the selection arguments to the public class FeedReaderDbHelper extends SQLiteOpenHelper { }9 method. It divides the selection specification into a selection clause and selection arguments. The clause defines the columns to look at, and also allows you to combine column tests. The arguments are values to test against that are bound into the clause. Because the result isn't handled the same as a regular SQL statement, it is immune to SQL injection. Kotlinpublic final class FeedReaderContract { }4 Javapublic final class FeedReaderContract { }5 The return value for the public class FeedReaderDbHelper extends SQLiteOpenHelper { }8 method indicates the number of rows that were deleted from the database. Update a databaseWhen you need to modify a subset of your database values, use the class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }5 method. Updating the table combines the private static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
6 syntax ofprivate static final String SQL_CREATE_ENTRIES = private static final String SQL_DELETE_ENTRIES =
7 with the val dbHelper = FeedReaderDbHelper(context) 4 syntax of public class FeedReaderDbHelper extends SQLiteOpenHelper { }8. Kotlinpublic final class FeedReaderContract { }6 Javapublic final class FeedReaderContract { }7 The return value of the class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { }5 method is the number of rows affected in the database. Persisting database connectionSince private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"5 and private const val SQL_CREATE_ENTRIES = private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${FeedEntry.TABLE_NAME}"6 are expensive to call when the database is closed, you should leave your database connection open for as long as you possibly need to access it. Typically, it is optimal to close the database in the val dbHelper = FeedReaderDbHelper(context) 9 of the calling Activity. Kotlinpublic final class FeedReaderContract { }8 Javapublic final class FeedReaderContract { }9 Debug your databaseThe Android SDK includes a FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getContext()); 0 shell tool that allows you to browse table contents, run SQL commands, and perform other useful functions on SQLite databases. For more information, see how to . |