Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

oncreate not called after creating database !

Tags:

android

Am creating a new database using helper, but as per the document on create should be called once the data base is created, but its not called properly. could any plz help me to resolve this asap. Plz see the code below.

1) Is there any way to create database instead of using helper if so plz advise me ! 2) What are the callbacks will be called in the database creation and also in kill of a database ?

OpenHelper(Context context) 
  {

     super(context, "examplee.db", null, 1 );
     SQLiteDatabase sqlite = null;       
      Log.w(TAG, "Openhelp database, ");
      sqlite =  context.openOrCreateDatabase("examplee.db", Context.MODE_PRIVATE, null );
      Log.e ( TAG,"SQ lite database object "+sqlite );                
  }

 public void onOpen(SQLiteDatabase db)
 {
     Log.e ( TAG,"On open called ");
 }


  @Override
  public void onCreate(SQLiteDatabase db) 
  {
      Log.w(TAG, " On create ");
      //db.execSQL(sql);
     //db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
  {
     Log.w(TAG, "Upgrading database, this will drop tables and recreate.");
     //db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
     //onCreate(db);
  }

}

Thanks in advance,

like image 496
Prasath Avatar asked May 23 '26 18:05

Prasath


2 Answers

Here's a very detailed description on how to delete a database file from your emulator (debugging on an actual android phone is very similar).

  1. Run your emulator. If you're having problems with that, then you can ignore the rest of this help message!

  2. Pull up a command-line interface, according to your operating system.

  3. Execute the command: 'adb shell' (omit the quotes, of course). This will take you into the Android Debug Bridge; your prompt will change to something like a simple pound sign. Regardless of your original operating system, you are now in a simplified unix OS.

  4. You are now in your emulator. Type the command 'ls -l' to verify that you're in the root directory (you'll see something that looks very much like a unix root directory system).

  5. Change directories to where your database file is stored. Suppose that the program that your ran is in the package com.sillynames.myprogram5, and the database file is called 'myblackbook.db'. You will find the file at the directory:

    /data/data/com.sillynames.myprogram5/databases/myblackbook.db

  6. Once you're at that directory, simply delete the database via 'rm myblackbook.db'.

Hope this helps! -scott

like image 75
SMBiggs Avatar answered May 25 '26 08:05

SMBiggs


The SQLiteOpenHelper javadoc says that onCreate is "Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.". It is not called everytime the application comes up. In your case Probably the db is already created.

To verify if the db exists login to the shell with adb and go to /data/data/<your application package name>/databases and see if the employees.db file exists there.

In the onCreate method typically you will create your tables and load any initial data. This is executed when the app is launched for the first time after installation.

like image 31
Madhusuthanan Seetharam Avatar answered May 25 '26 08:05

Madhusuthanan Seetharam