Android Tutorial

(avery) #1
Android Tutorial 301

Here we focus on creating databases within our Android
applications. For that, we use the built-in SQLite support to
programmatically create and use a SQLite database to store
application information. However, if your application works with a
different sort of database, you can also find more generic database
classes (within the android.database package) to help you work
with data from other providers.


In addition to programmatically creating and using SQLite
databases, developers can also interact directly with their
application’s database using the sqlite3 command-line tool that’s
accessible through the ADB shell interface. This can be an
extremely helpful debugging tool for developers and quality
assurance personnel, who might want to manage the database
state (and content) for testing purposes.


Creating a SQLite Database


You can create a SQLite database for your Android application in
several ways. To illustrate how to create and use a simple SQLite
database, let’s create an Android project called SimpleDatabase.


Creating a SQLite Database Instance Using the Application
Context


The simplest way to create a new SQLiteDatabase instance for your
application is to use the openOrCreateDatabase() method of your
application Context, like this:

Free download pdf