Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:38:51+00:00 2026-06-18T14:38:51+00:00

This is basically my first android app and I have been trying to use

  • 0

This is basically my first android app and I have been trying to use a prefilled sqllite database to work out my requirements.

My problem is that onCreate function does not gets called.

I have taken bits of codes from many places and combined them to form this class.

Updating prepopulated database in Android

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

And some others from android.com

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Random;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

    SQLiteDatabase db;
    private static final String TAG = "DataBaseHelper";
    int id = 0;
    Random random = new Random();
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    // Constructor
    public DatabaseHandler(Context context) {
        super(context, DefinitionContract.DATABASE_NAME, null, DefinitionContract.DATABASE_VERSION);
        Log.d(TAG, "DatabaseHandler constructor called\n");
        db = getWritableDatabase();
        this.myContext = context;
        //createDB();
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        Log.d(TAG, "onCreate called\n");
        createDB();
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG, "onUpgrade called\n");
        Log.w(TAG, "Upgrading DB from version " + oldVersion + " to " +
                    newVersion + ", which will destroy all old data");

        // Drop older table if existed
        String sql  = "DROP TABLE IF EXISTS " + DefinitionContract.CATEGORY_TABLE_NAME;
        db.execSQL(sql);

        sql = "DROP TABLE IF EXISTS " + DefinitionContract.CONTENTS_TABLE_NAME;
        db.execSQL(sql);

        // Create table again
        onCreate(db);
    }

    public void createDataBase(SQLiteDatabase db) {
        Log.d(TAG, "createDataBase called\n");
        createDB();
        db.execSQL(DefinitionContract.CREATE_CATEGORY_TABLE);
        db.execSQL(DefinitionContract.CREATE_CONTENTS_TABLE);
    }

    private void createDB() {
        Log.d(TAG, "createDB called\n");
        boolean dbExist = dbExists();
        Log.d("SQL Helper", "Condition:\n");
        if(!dbExist) {
            Log.d("SQL Helper", "Condition 1\n");
            copyDataBase();
        } else if(dbExist) {
            Log.d("SQL Helper", "Condition 2\n");
            copyDataBase();
        }
    }

    private boolean dbExists() {
        Log.d(TAG, "dbExists called\n");
        //File dbFile = new File(DefinitionContract.DATABASE_PATH + DefinitionContract.DATABASE_NAME);
        //return dbFile.exists();

        SQLiteDatabase db = null;
        try {
            String dbPath = DefinitionContract.DATABASE_PATH + DefinitionContract.DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
            db.setLocale(Locale.getDefault());
            db.setLockingEnabled(true);
            db.setVersion(DefinitionContract.DATABASE_VERSION);
        }
        catch(SQLiteException e){
            Log.e("SQL Helper", "database not found");
        }
        if(db != null) {
            db.close();
        }
        return db != null ? true : false;
    }

    private void copyDataBase() {
        Log.d(TAG, "copyDataBase called \n");
        InputStream iStream = null;
        OutputStream oStream = null;
        String outFilePath = DefinitionContract.DATABASE_PATH + DefinitionContract.DATABASE_NAME;
        try{
            iStream = myContext.getAssets().open(DefinitionContract.DATABASE_NAME_EXT);
            oStream = new FileOutputStream(outFilePath);
            byte[] buffer = new byte[1024];
            int length;
            while((length = iStream.read(buffer))>0) {
                oStream.write(buffer,0,length);
            }
            oStream.flush();
            oStream.close();
            iStream.close();
        }
        catch(IOException ioe){
            throw new Error("Problem copying database from resource file.");
        }
    }

    public void openDataBase() throws SQLException {
        String myPath = DefinitionContract.DATABASE_PATH + DefinitionContract.DATABASE_NAME_EXT;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public synchronized void close() {
        Log.d(TAG, "close called\n");
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

    public void readDB() {
        Log.d(TAG, "readDB called\n");
        String selectQuery = "SELECT  * FROM " + DefinitionContract.DATABASE_ONLYNAME+"."+DefinitionContract.CATEGORY_TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                int arraySize = cursor.getColumnCount();
                String newlog   = "arraySize=" + arraySize + "*****";
                for(int i = 0; i < arraySize; i++) {
                    newlog  = newlog + cursor.getString(i)+ "\n";
                }

                Log.d("Details: ", newlog);
            } while (cursor.moveToNext());
        }
    }

}

I have already done a google search and tried to fix it but it doesn’t work yet.

Android SQLiteOpenHelper : onCreate() method is not called. Why?

SQLiteOpenHelper failing to call onCreate?

LogCat showns only “DataBaseHandler constructor called” and then “shutting down VM” after that

But if I remove the db = getWritableDatabase(); line from my constructor it works further and shows that the readDB() function is being called.

Some definitions that might help in understanding my code:

public static final String DATABASE_NAME        = "mydb.db";

public static final String DATABASE_NAME_EXT        = "mydb.sqllite";

// Database path
public static final String DATABASE_PATH = "/data/data/com.abhishekgdotcom.collection/databases/";

Any help guys?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:38:52+00:00Added an answer on June 18, 2026 at 2:38 pm

    Well, the real reason why onCreate() method wasn’t working for me because it is called only the first time the app runs. Or when you change the DATABASE_VERSION then onUpgrade gets called which in turn calls the onCreate method again.

    I believe this is the default android working.

    P.S I could get the onCreate working everytime by either changing the database version or deleting the database file from /data/data/packagename/databases/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get my first Android service to work. I have this
I am trying to create my first Android app and need some help. Basically
I have basically finished developing an android app that makes use of SQLite databases
I'm currently developing my first android app, and my first game. I've been developing
this is my first time using the ListView and have have tried out many
I'm trying to learn android and for my app i have few questions. If
I am trying to create a simple test app that basically extends the Android
I've been trying to figure this out for a while now and I can't
Hiya I am very new to C#, infact this is basically my first task
This is the first time I got this error. This code basically gets the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.