Android provides an elegant interface for your app to
interact with an SQLite database. To access the database, you first need
a helper class that provides a “connection” to the database, creating
the connection if it doesn’t already exist. This class, provided to you
by the Android framework, is called
SQLiteOpenHelper. The database class it
returns is an instance of
In the following
subsections I’ll explain some of the background concepts you should
understand when working with
DbHelper. I’m not going to explain SQL or
basic database concepts such as normalization, because there are
hundreds of good places to find that information, and I expect most of
my readers already know it. However, this chapter should give you enough
to get started, even if your knowledge of databases is spotty.
A schema is just a description of what’s in a database. In our Yamba database, for instance, we want fields for the following information about each tweet we retrieve from Twitter:
The date when the tweet was sent
The text of the tweet
The user who sent the tweet
So each row in our table will contain the data for one tweet, and these four items will be the columns in our schema, along with a unique ID for each tweet. We need the ID so we can easily refer to a tweet. SQLite, like most databases, allows us to declare the ID as a primary key and even assigns a unique number automatically to each tweet for us.
The schema has to be ...