简体   繁体   中英

Android - SQLiteDatabase Not Maintaining Insert

Using adb + sqlite3 (the tool), and querying the database after every action, I have an app that inserts a row into my_conference via:

ContentValues values = new ContentValues();

      values.put("ITEM_ID", itemId);
      values.put("ITEM_DATE", sessionDate);

      mDb.beginTransaction();
      mDb.insert("my_conference", null, values);
      mDb.setTransactionSuccessful();
      mDb.endTransaction();

I can then query the database and make sure the row is there. Then, when I hit the back button on the device and after the activity returns to the screen before it, I requery the database and it is empty, devoid of the row inserted. I'm not doing anything funny to my DB in onDestroy(), just calling db.close();

What gives?

EDIT: In an attempt to make sure there were no errors, and combining what else I've found by googling, I came up with this, same thing though, inserts, then removes after onDestroy():

ContentValues values = new ContentValues();

      values.put("ITEM_ID", itemId);
      values.put("ITEM_DATE", sessionDate);

      try
      {
        mDb.beginTransaction();
        mDb.insertOrThrow("my_conference", null, values);
        mDb.setTransactionSuccessful();
      }
      catch( Exception e )
      {
        Log.e("DB", "EXCEPTION: " + e.getMessage() );
      }
      finally
      {
        mDb.endTransaction();
      }

So it turns out I had a bad database exists check, that always thought it didn't exists, so the database was being created anew on each activity load...

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