Wednesday, 15 January 2014

java - How to use getWritableDatabase() in onCreate function of SQLite database? -


I'm writing an Android app. For this, I have to populate a SQLite database with data from a txt file. Therefore, in the on-going function of the database, I am creating the database and then I am populating it with the data. It is like the declaration of onCreate's declaration:

  Creating a Public Zero (SQLiteDatabase db) {  

But when I create this line inside the creature I use:

  db = this.getWritableDatabase (); I get this error in the logger in runtime: "java.lang.IllegalStateException: getDatabase recursively (812)". So now I am unable to populate the database from inside, I am creature and trapped. Any help would be appreciated.   

onCreate () gets the call for the first time, which is what you call getrereadableDatabase or getWriteableDatabase It is being said that the crate is being called repeat because DB is not yet created and your

  db = this.getWritableDatabase ();   

The call triggers creation within creation.

If you have to prefill the DB, just use the DB logic of the onCreate as a writable database.

  @Override Creating a Public Zero (SQLiteDatabase DB) {db.execSQL (YOUR_STATEMENT); . . }   

If you need to do many DB operations, then using

  db.beginTransaction (); . . Db.setTransactionSuccessful (); } Hold (SQLException e) {} Finally {db.endTransaction (); }   

Generally fast.

No comments:

Post a Comment