O'Reilly logo

Learning Android by Marko Gargenta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

DbHelper

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 SQLiteDatabase.

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.

The Database Schema and Its Creation

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:

created_at

The date when the tweet was sent

txt

The text of the tweet

user

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required