简体   繁体   中英

Retrieving ID from sqlite database

Relating to my previous question, I want to retrieve the ID from the sqlite database created to pass it as data in an Intent putExtra() for using it as the id for pendingIntent.getActivity(). Currently, I am using queryNumEntries to set the id for each reminder added into the database from the following code in my DatabaseManager.class:

public long reminderCount(){
        SQLiteDatabase sqlDB = this.getReadableDatabase();
        return DatabaseUtils.queryNumEntries(sqlDB, DatabaseReminder.TABLE_NAME);
    }

and calling it in AddActivity.class

String remindId;
long RemID;
int remId;

RemID = databaseManager.reminderCount()+1;
remindId = String.valueOf(RemID);                
remId = Integer.parseInt(remindId);

private void sendUpdateAlarm() {
        Intent updateIntent = new Intent(getApplicationContext(), UpdateReminderActivity.class);
        updateIntent.putExtra("NotifID", remId);
    }

    private void setAlarm() {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(getApplicationContext(), ReminderReceiver.class);
        intent.putExtra("DateTime", dateTimePicked);
        intent.putExtra("NotifID", remId);
        intent.putExtra("Title", titlePicked);
        intent.putExtra("Description", descriptionPicked);

        PendingIntent addIntent = PendingIntent.getBroadcast(this, remId, intent, 0);
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, Date.parse(dateTimePicked), addIntent);
    }

I am confused if there is another method to retrieve the ID right when the entry is created aside from trying this.

Update: In insert method for DatabaseManager.class

Intent intent = new Intent(context, AddReminderActivity.class);
intent.putExtra("remindId", reminderID);

AddReminderActivity:

                RemID = getIntent().getLongExtra("remindId", 0);
//                Log.i(TAG, "remID: " + RemID);
//                = 0
                reminderId = String.valueOf(RemID);
//                Log.i(TAG, "reminderId: " + reminderId);
//                = 0
                remId = Integer.parseInt(reminderId);
//                Log.i(TAG, "reminder: " + remId);
//                = 0

I am confused if there is another method to retrieve the ID right when the entry is created aside from trying this.

Yes, BUT it depends upon the definition of the table.

If the id is generated then when inserting the row (entry) and if using the convenience SQLiteDatabase insert method (or the insertOrThrow method or the insertWithOnconflict method) then the long that is return will be the generated id or -1 if the row was not inserted.

An id will be generated if the column is defined using INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT . (see links below for an explanation)

Using your method of assuming that the new id will be 1 greater than the number of rows in the table will not be accurate under a number of scenarios, such as if any row other than the last has been deleted.

A query less prone to resulting in another value would be to use coalesce(max(id),0) as this gets the highest id, irrespective of deleted rows.

However using the max aggregate function, the value returned can also be incorrect, such as if rows are inserted with an id that is less than the highest. This is possible if

  • the id value has been specified when inserting or
  • the highest allowable (9223372036854775807) id has been used and AUTOINCREMENT has not been coded in the column definition (if AUTOINCREMENT has been coded then in the case that the highest allowable id has been used then an SQLITE_FULL error will result)

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