【问题标题】:Retrieving ID from sqlite database从 sqlite 数据库中检索 ID
【发布时间】:2022-06-13 18:58:41
【问题描述】:

关于我之前的问题,我想从创建的 sqlite 数据库中检索 ID,以将其作为 Intent putExtra() 中的数据传递,以将其用作 pendingIntent.getActivity() 的 ID。目前,我正在使用 queryNumEntries 为从我的 DatabaseManager.class 中的以下代码添加到数据库中的每个提醒设置 id:

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

并在 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);
    }

除了尝试这个之外,是否还有其他方法可以在创建条目时检索 ID。

更新: 在 DatabaseManager.class 的插入方法中

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

【问题讨论】:

    标签: java sqlite android-studio android-sqlite


    【解决方案1】:

    我很困惑,除了尝试这个之外,是否还有其他方法可以在创建条目时检索 ID。

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

    如果 id 是在插入行(条目)时生成的,并且如果使用方便的SQLiteDatabase insert 方法(或insertOrThrow 方法或insertWithOnconflict 方法),那么长is return 将是生成的 id 或 -1 如果没有插入行。

    如果使用INTEGER PRIMARY KEYINTEGER PRIMARY KEY AUTOINCREMENT 定义列,则会生成一个id。 (有关说明,请参阅下面的链接)

    使用假设新 id 将比表中的行数大 1 的方法在许多情况下是不准确的,例如,如果除最后一行之外的任何行已被删除。

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

    但是使用 max 聚合函数,返回的值也可能不正确,例如插入的行的 id 小于最高值。如果

    • 插入或时已指定id值
    • 允许的最高 (9223372036854775807) id 已被使用,并且 AUTOINCREMENT 尚未在列定义中编码(如果 AUTOINCREMENT 已被编码,那么在使用了最高允许 id 的情况下,则将导致 SQLITE_FULL 错误)

    【讨论】:

    • 对于插入的 ID 值,我在创建表和插入条目时使用了不使用 AUTOINCREMENT。根据您的回答,除了使用 coalesce() 或 max_agg() 之外,rawQuery 是否可以获取为每个条目选择的 id 以用作 pendingIntent 中的 id?
    • @AmethystLilac 不作为单个语句/调用,只有便捷方法会这样做,因为它们会执行额外的工作来检索 id(实际上,即使您没有指定整数主键,rowid 也会返回rowid)。但是,您可以在插入之后立即使用rawQuery(在插入安全的事务中),插入可以使用 rawQuery 或 execSQL(后者因为没有从这样的插入返回结果集)。跨度>
    • 所以在使用'databaseManager.addReminder(titlePicked, descriptionPicked, timePicked, datePicked, dateTimePicked);'之后我是否在我的 DatabaseManager.class 以及函数中指定的这一行之后立即调用 rawQuery 方法?
    • @AmethystLilac 这并不完全安全(可能是但可能会插入/更新/删除另一行),但我真的建议使用方便的插入方法插入,它可以满足您在事务从而锁定任何其他潜在的插入/更新/删除。附言以上不考虑WITHOUT ROWID 表(se sqlite.org/withoutrowid.html
    • 按照我的 DatabaseManager.class 中的 insert 方法如下: );长提醒ID = sqlDB.insert(DatabaseReminder.TABLE_NAME, null, CV); if (reminderID > 0){ Toast.makeText(context, "提醒已添加!", Toast.LENGTH_SHORT).show(); } sqlDB.close(); } 所以只要reminderID不小于等于0,还会不安全吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    相关资源
    最近更新 更多