RSS Feed

Some Things You Need To Know About Oncreate, Database Helper And GetWriteableDatabase

This is an issue that held me up for some time. Evidently it is logical now I understand it.

This is an extremly good clear explanation by Mina Samy. It lacks how it is called from a higher class so I have included some sample code for this below.

The OnCreate method in the SQLiteOpenHelper (that you should extend from) IS ONLY CALLED ONCE. NOT ONCE EVERY TIME YOUR APP IS CREATED, BUT ONCE FOR THE CREATION OF THE DATABASE. It will not be created again unless the DATABASE VERSION changes. First this is logical, why should you want to create a database each time you start. But second it gives you a hard time when trying to debug your code,

A few important points. onCreate will be called if you don’t have a line like below calling getWritableDatabase(). Later you will use getWritableDatabase() when you need to insert, amend or delete table rows. When I write this.


public class MainActivity extends Activity

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
setContentView(new MyView(this));

DatabaseCreate db = new DatabaseCreate(this);

As far as I can see until you call this you  will not execute the onCreate method of your SQLiteOpenHelper extended class. Note as stated before your oncreate aslo will not be called unless your database version has changed. Note where I call it. On many of the examples on the web it misses this point.

SQLiteDatabase sb = db.getWritableDatabase();



Now you must “fool” the running app to think that it has never created that database, particularly when you are debugging and developing the database. Once the database has settled you won’t be calling this code. Later when you issue updates to your live database you will be using the onUpdate() method.

First useful technique is to change the database version..

public DatabaseCreate(Context context)

STEP 3 – Using the emulator

I like to start the emulator before I test my code rather than do it together. Otherwise I always get an error because the emulator takes so much time to start.

Your DATABASE on the emulator is held in memory. Hence, it seems the difficulty in debugging database applications. However, you can pull the database and read it with Firefox or something like SQLite Expert. See this link for one way of pulling your database and examining it (for emulator only). ‎

STEP 3 – Using a hardware device

This is a little easier. All you have to do is find your database on your device and delete it. The database is stored here.. data/data/com.your.application/databases/databasename

Please tell me if I helped you. It will encourage me to add more knowledge to my blogg. 🙂


Leave a Reply

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

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

Google photo

You are commenting using your Google 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: