Enter your keyword

Android SQLite Database Tutorial

Android SQLite Database Tutorial

Android SQLite Database Tutorial

Our Schema

One 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.  Below is the sample schema for the sqlite database that we will be using in this tutorial.

Now we shall create a companion model class, known as a Contact class, which explicitly specifies the layout of your schema in a systematic and self-documenting way.

Create a Database Using a SQL Helper

The ContactHelper 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 ContractHelper 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 that enumerates its columns.

Note: By implementing the BaseColumns interface, your inner class can inherit a primary key field called _ID that some Android classes such as cursor adaptors will expect it to have. It’s not required, but this can help your database work harmoniously with the Android framework.

Once 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:

Add new contact to the database

Insert data into the database by passing a ContentValues object to the insert() method:

Retrieve contact(s) from the database

To read from a database, use the query() method, passing it your selection criteria and desired columns. The method combines elements of insert() and update(), except the column list defines the data you want to fetch, rather than the data to insert. The results of the query are returned to you in a Cursor object.

Update  a contact in the database

When you need to modify a subset of your database values, use the update() method.

Updating the table combines the content values syntax of insert() with the where syntax of delete().

Delete Information from the database

To delete rows from a table, you need to provide selection criteria that identify the rows. The database API provides a mechanism for creating selection criteria that protects against SQL injection. The mechanism 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.

Away from using only the SQLiteDatabase class methods we have looked at, i.e., insert(), query(), update(), and delete(), you might be interested in using the rawQuery() and execSQL() methods, and the SQLiteQueryBuilder class.

  • rawQuery() directly accepts an SQL select statement as input.
  • execSQL() allows to execute an SQL statement directly.
  • SQLiteQueryBuilder is a convenience class that helps to build SQL queries.

Now that we have all the database related code ready, let’s get down to using this SQLite database in our Android application in part II of this tutorial.