Prasath
Prasath

Reputation: 1121

oncreate not called after creating database !

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,

Upvotes: 3

Views: 4944

Answers (4)

Kisakyamukama
Kisakyamukama

Reputation: 109

Make sure you await to get the database reference. Check if you have the database reference that was my issue. Had declared Database _db on top of class but in my functions it was null

 final Database db = await initDb();
 print("insert to db $db");

initDB

Future<Database> initDb() async {
print("called db init");
try {
  final dbFolder = await getDatabasesPath();

  if (!await Directory(dbFolder).exists()) {
    print("db path does not exist");
    await Directory(dbFolder).create(recursive: true).then((value) {
      print("db directory $value");
    });
  }

  final dbPath = join(dbFolder, _kBdFileName);
  print("db path $dbPath");

  // open db
  _db = await openDatabase(
    dbPath,
    version: 1,
    onCreate: (db, version) async {
      print("Call on create");
      await _initDBtables(db).then((value) {
        print("on created ");
      });
    },
  );
  print("Db initialized $_db");
  // success init db
  return _db;
} on DatabaseException catch (e) {
  print(e);

  return _db;
}

}

Upvotes: 0

SMBiggs
SMBiggs

Reputation: 11688

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

Upvotes: 5

Vikas Patidar
Vikas Patidar

Reputation: 43349

Is there any way to create database instead of using helper if so plz advise me !

Yes you can create your databse manually on your system using the the tool SQLite Manager or other and bundle that sqlite db file in your assets folder of your project. And when your application run then you need to copy that database file to the path /data/data/your_app_package_name/databases/

Refer this : Using your own SQLite database in Android applications

What are the callbacks will be called in the database creation and also in kill of a database ?

When your database is created fist time the onCreate() method is called. And there is no way to kill/drop a database instead you can delete that db file.

Upvotes: 0

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.

Upvotes: 2

Related Questions