从SQLITE数据库检索ID
关于我以前的问题,我想从创建的sqlite数据库中检索ID,以将其作为数据传递为putextra()中的数据,以将其用作pendingIntent.getActivity()的ID。 设置每个提醒的ID
public long reminderCount(){
SQLiteDatabase sqlDB = this.getReadableDatabase();
return DatabaseUtils.queryNumEntries(sqlDB, DatabaseReminder.TABLE_NAME);
}
当前
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);
}
,我正在使用QueryNumentries来 除了尝试此操作外,还创建了。
更新: 在databasemanager的插入方法中
Intent intent = new Intent(context, AddReminderActivity.class);
intent.putExtra("remindId", reminderID);
。
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,但这取决于表的定义。
如果生成 id ,则在插入行(条目)时,以及使用便利 sqlitedatabase insert in e method 方法(或insertOrThrow方法或 insertwithonConflict 方法)然后,如果不插入该行,则返回的长度将是生成的ID或-1。
如果使用
Integer主键
或Integer主密钥自动启动
定义列,则将生成 ID 。 (有关说明,请参见下面的链接)使用您假设新方法的方法在许多方案下,ID将比表中的行数大1个,例如,除了最后一个方案以外的任何行都被删除。
不太容易产生另一个值的查询将是使用
coce(max(id),0)
,因为这获得了最高的ID,而与已删除的行无关。,返回的值也可能是不正确的,例如,用小于最高的 id 插入行。 ,则可以使用这一点
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
orINTEGER 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