简体   繁体   中英

My database deletes all rows after onDestroy() is called

I'm implementing the content provider's abstract method "insert" to fill my database from an online source.

I can run queries using the content provider fine, but when I clear the app from memory and restart, the content provider queries return empty because the database seems empty.

here is my abstract insert...

@Override
public Uri insert(Uri uri, ContentValues contentValues) {
  if(!contentValues.containsKey("_id")) {
    // produce UUID
    contentValues.put("_id", UUID.randomUUID().toString());
  }

  long rtrn = db.insertWithOnConflict(DatabaseHelper.DATABASE_TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
  String query = "SELECT _id, name, favourite FROM recipe";
  Cursor cursor = db.rawQuery(query, null);
  Log.d("D", "running cursor.getCount(): "+cursor.getCount());
  cursor.close();
  getContext().getContentResolver().notifyChange(uri, null);
  return uri;
}

the logcat shows the running cursor count to be the same as the "rtrn" row index returned by the insertWith. So the data is being entered successfully.

however in my fragment...

  SQLiteDatabase db = new DatabaseHelper(context).getWritableDatabase();
  String query = "SELECT _id, name, favourite FROM recipe";
  Cursor cursor = db.rawQuery(query, null);
  Log.d("D", "getCount(): "+cursor.getCount());
  cursor.close();

...it reports an empty database after onDestroy() is called and the app is restarted.

How can I have my database contents persist? Is the data there and I'm not accessing it properly? How do I access the database after my app calls the onDestroy() and restarts?

I ran the following debug code in onPause() and onStop() and onDestroy().

Cursor cursor = this.getActivity().getContentResolver().query(uri, null, null, null, null);
Log.d("D", "Big getCount(): "+cursor.getCount());
cursor.close();

In the logcat... onPause() and onStop() report 11 rows from the database query. My Log.d did not show in onDestroy().

When I clear the program from memory and restart, the code returns zero rows again. The database is empty.

It looks like you are actually recreating the database each time onCreate is called. DataProvider:

public boolean onCreate() {
      db = new DatabaseHelper(getContext()).getWritableDatabase();
      return false;
    }

DatabaseHelper:

private static final String DATABASE_CREATE =
    "create table recipe (_id char(36) primary key, name text null, instructions text null, favourite tinyint not null default 0, sync tinyint not null default 0, del tinyint not null default 0);";

@Override
public void onCreate(SQLiteDatabase db) {
  Log.d("D", "creating database!");
  db.execSQL(DATABASE_CREATE);
}

What is the transaction management mechanism? Do you use manual transactions or automatic transactions?

If you use manual transactions ie you use statement "BEGIN TRANSACTION", do make sure that your code has a "COMMIT" at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM