1

Need some help in using databases in Android.

I am trying to insert data into a db provided by MoEngage from doWakefulWork() inside WakefulIntentService.

Here's my function to insert data into db :

private void addToDb(Bundle extras) {
    String msgDetails = "my string";
    ContentValues values = new ContentValues();
    if (null != msgDetails) {
        values.put("msg", msgDetails);
    }

    Uri newRecord = App.context.getContentResolver().insert(MoEDataContract.MessageEntity.getContentUri(App.context),
            values);
    App.context.getContentResolver().notifyChange(newRecord, (ContentObserver) null);

    if (MoEHelperUtils.isDebugEnabled()) {
        if (null != newRecord) {
            Log.d(MoEHelper.TAG, "PushMessagingListener: added new record with entry: " + newRecord);
        } else {
            Log.d(MoEHelper.TAG, "PushMessagingListener: FAILED to add new record with entry: ");
        }
    }
}

However, when I open the app and try to query the database, the cursor returns 0 count.

If I try to query right after adding values inside doWakefulWork() only, the cursor shows the value in database.

Cursor cursor = MoEController.getAllMessages(App.context);

Am I missing somethings here? Any help would be appreciated. Thanks!

Mykola
  • 3,343
  • 6
  • 23
  • 39
nipun.birla
  • 719
  • 5
  • 19

2 Answers2

1

Please take a look at https://stackoverflow.com/a/3486373/1349601

It looks like you should create a transaction and mark it as successful.

Community
  • 1
  • 1
g90
  • 506
  • 3
  • 18
0

Here are examples on how to insert a data on Android by ContentValue:

The Context can be either null or not. I found this possibility because the Context is null in the sample code about inserting data in Github. So, I have assumed that maybe the ‘App’ is using Singleton pattern.

You may check out this link.

This won't work if your SQLiteOpenHelper is used by a Content Provider, because the Content Provider's onCreate() is called before the Application's onCreate().

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59