atabase support is the lifeline of every application, big or small. Unless your application deals only with simple data, you need a database system to store your structured data. Android uses the SQLite database system, which is an open-source, stand-alone SQL database, widely used by many popular applications. For example, Mozilla Firefox uses SQLite to store configuration data and iPhone also uses SQLite for database storage.
In Android, the database that you create for an application is only accessible to itself; other applications will not be able to access it. Once created, the SQLite database is stored in the /data/data/<package_name>/databases folder of an Android device. In this article, you will learn how to create and use a SQLite database in Android.
Create an Android project using Eclipse and name it Database
(see Figure 1
|Figure 1. Database: Your new Android project, created using Eclipse.|
Creating the DBAdapter Helper Class
A good practice for dealing with databases is to create a helper class to encapsulate all the complexities of accessing the database so that it's transparent to the calling code. So, create a helper class called DBAdapter that creates, opens, closes, and uses a SQLite database.
First, add a DBAdapter.java file to the src/<package_name> folder (in this case it is src/net.learn2develop.Database).
In the DBAdapter.java file, import all the various namespaces that you will need:
public class DBAdapter
|Figure 2. The Database Fields: This shows the titles table you will be building in this article.|
Next, create a database named bookstitles with the fields shown in Figure 2.
In the DBAdapter.java file, define the following constants shown in Listing 1.
The DATABASE_CREATE constant contains the SQL statement for creating the titles table within the books database.
Within the DBAdapter class, you extend the SQLiteOpenHelper classan Android helper class for database creation and versioning management. In particular, you override the onCreate() and onUpgrade() methods (as shown in Listing 2).
The onCreate() method creates a new database if the required database is not present. The onUpgrade() method is called when the database needs to be upgraded. This is achieved by checking the value defined in the DATABASE_VERSION constant. For this implementation of the onUpgrade() method, you will simply drop the table and create the table again.
You can now define the various methods for opening and closing the database, as well as the methods for adding/editing/deleting rows in the table (see Listing 3).
Notice that Android uses the Cursor class as a return value for queries. Think of the Cursor as a pointer to the result set from a database query. Using Cursor allows Android to more efficiently manage rows and columns as and when needed.
You use a ContentValues object to store key/value pairs. Its put() method allows you to insert keys with values of different data types.
The full source listing of DBAdapter.java is shown in Listing 4.