Android Basics – Learn how to use the SQLiteOpenHelper class to setup App Database

SQLiteOpenHelper enables you to create and manage the database. To understand this better create a new class called SurveyDBHelper and extend this class.

In this class you will write the code to create the database tables needed for the Survey App.

public class SurveyDBHelper extends SQLiteOpenHelper

The Survey App will store the information in a survey table. The survey table will store – name, email and age.

It’s an Android convention to call your primary key columns _id.

Following are the data types that can be used

INTEGER – Numbers
TEXT – Character
REAL – Floating Points
NUMERIC – Booleans and Date
BLOB – Binary large object

Keeping this in mind you can use the CREATE TABLE SQL statement to create a new table.

@Override
public void onCreate(SQLiteDatabase db) {
 db.execSQL("CREATE TABLE "+SURVEY_TABLE+" (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
 +SURVEY_TABLE_NAME_COLUMN+" TEXT, "
 +SURVEY_TABLE_EMAIL_COLUMN+" TEXT, "
 +SURVEY_TABLE_AGE_COLUMN+" INTEGER);"
 );
}

Follow the video to understand this better. 
Also the source code is available here for reference.


Author: Ankur

I am Enthusiastic about Learning new things and sharing my Knowledge. I like programming and have a pretty good background in Computer Science.

One thought on “Android Basics – Learn how to use the SQLiteOpenHelper class to setup App Database”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: