简体   繁体   English

Android - SQLiteDatabase 不维护插入

[英]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:使用 adb + sqlite3(工具),并在每次操作后查询数据库,我有一个应用程序通过以下方式将一行插入 my_conference:

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();我没有在 onDestroy() 中对我的数据库做任何有趣的事情,只是调用 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():编辑:为了确保没有错误,并结合我通过谷歌搜索发现的其他内容,我想出了这个,同样的事情,插入,然后在 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...所以事实证明我有一个错误的数据库存在检查,一直认为它不存在,所以每次活动加载时都会重新创建数据库......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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