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: androidsmartly

I am a Tech Enthusiast who likes to work on new Ideas. Sharing knowledge on Android, Marketing, Startups and More.

1 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 )

Google+ photo

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

Connecting to %s