从SQLITE数据库检索ID

发布于 2025-01-31 15:49:33 字数 1956 浏览 1 评论 0 原文

关于我以前的问题,我想从创建的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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£噩梦荏苒 2025-02-07 15:49:33

我是否会感到困惑,如果除了尝试此操作之外,还有另一种方法可以正确检索ID。

是的,但这取决于表的定义。

如果生成 id ,则在插入行(条目)时,以及使用便利 sqlitedatabase insert in e method 方法(或insertOrThrow方法或 insertwithonConflict 方法)然后,如果不插入该行,则返回的长度将是生成的ID或-1。

如果使用 Integer主键 Integer主密钥自动启动定义列,则将生成 ID 。 (有关说明,请参见下面的链接)

  • 请注意,只有整数关键字具有此效果。例如,主键将行不通。
  • 主键可以在列级(如图所示),也可以在表级别。
  • 有关说明ary_key 以及 https://sqlite.org/autoinc.html

使用您假设新方法的方法在许多方案下,ID将比表中的行数大1个,例如,除了最后一个方案以外的任何行都被删除。

不太容易产生另一个值的查询将是使用 coce(max(id),0),因为这获得了最高的ID,而与已删除的行无关。

,返回的值也可能是不正确的,例如,用小于最高的 id 插入行。 ,则可以使用这一点

  • 时指定了ID值
  • 如果在插入或最高允许(9223372036854775807)插入或最高允许 。已经使用了最高允许的ID,然后将导致SQLite_full错误)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文