- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 Android/SQLite 中使用 insertWithOnConflict 函数来插入可能已经存在于数据库中的值。根据 Android 文档,如果该值存在,将返回主键:
Returns
the row ID of the newly inserted row OR the primary key of the existing row if
the input param 'conflictAlgorithm' = CONFLICT_IGNORE OR -1 if any error
但是,使用该函数时,如果行已经存在,则不会返回现有的主键。相反,它会自动增加主键并返回该值,即使没有数据插入到返回的 id 的行中。
函数调用本身比较简单:
name_id = db.insertWithOnConflict("names", null, name_values, SQLiteDatabase.CONFLICT_IGNORE);
最佳答案
我也遇到过。我还看到了一些更奇怪的情况,它也返回了 0。似乎 API 级别存在一些问题。已经恢复到 insertOrThrow 并且那对我有用。
关于Android insertWithOnConflict 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8663745/
我正在重构我的代码并将所有 insert 语句替换为 insert or replace 而我发现以下方法使用 db.insert() 而不是准备好的语句: public static void in
我在 Android/SQLite 中使用 insertWithOnConflict 函数来插入可能已经存在于数据库中的值。根据 Android 文档,如果该值存在,将返回主键: Returns th
我正在使用 SQLiteDatabase.CONFLICT_IGNORE 调用 insertWithOnConflict。但是,当发生冲突时,将返回“-1”而不是现有行的 ID。我该如何纠正这个问题?
我试图了解 SQLiteDatabase.insertWithOnConflict 的返回值。 我的代码是这样的: long result = db.insertWithOnConflict(
我需要插入或更新,我找到了 SQLiteDatabase 的 insertWithOnConflict 方法,但我不知道它如何检查该条目是否已存在。 理论上我需要一个“Where”参数来检查某个 ID
是否有一些简单的方法(比建立自己的查询更容易),如何包含 SQLiteDatabase.insertWithOnConflict(); 从 v7 开始在 Android API 中? 据我所知,这是从
我有一个 SQLite 表: CREATE TABLE regions (_id INTEGER PRIMARY KEY, name TEXT, UNIQUE(name)); 还有一些安卓代码: Va
在我的sqlite数据库中,我在应用程序中使用了insertWithOnConflict方法来忽略我的sqlite数据库inn android应用程序中的重复数据条目。但我不知道它在我的代码中不起作用
问题背景:我有两个表,其中主键是文本字段。 当我对相同的数据重复使用 SQLiteDatabase.insertWithOnConflict(myTable, null, myValues, SQLi
我是一名优秀的程序员,十分优秀!