gpt4 book ai didi

android - 如何在不使用 CONFLICT_REPLACE 的情况下获得 "insert or update"行为?

转载 作者:行者123 更新时间:2023-11-29 16:11:03 25 4
gpt4 key购买 nike

我的 Android 应用程序使用 SQLite FTS3 表来提供全文搜索。我正在使用 insertWithOnConflictCONFLICT_REPLACE 来更新我的数据库,如果需要则插入新行或更新现有行(如果存在)。

我很惊讶地发现我的表最终包含重复的行——但看起来这是 SQLite 的 FTS 模块的文档化“功能”:

来自SQLite FTS3 and FTS4 Extensions页:

Datatypes and column constraints are specified along with each column. These are completely ignored by FTS and SQLite.

从命令行复制副本非常容易:

sqlite> CREATE VIRTUAL TABLE test_duplicates USING FTS3
...> (id INTEGER PRIMARY KEY, name TEXT);
sqlite> INSERT INTO test_duplicates (id, name) VALUES (1, "George");
sqlite> INSERT INTO test_duplicates (id, name) VALUES (1, "George");
sqlite> INSERT OR REPLACE INTO test_duplicates (id, name) VALUES (1, "George");
sqlite> SELECT * FROM test_duplicates;
1|George
1|George
1|George
sqlite>

我的问题是:复制 CONFLICT_REPLACE 行为的最佳(最简单、最可靠)方法是什么?

我目前的想法是 (A) 执行 SELECT,然后根据结果执行 UPDATE 或 INSERT,或者 (B) 盲目地尝试删除现有行(可能存在也可能不存在)然后 INSERT。

最佳答案

引用fts document ,我找到了这一段:

... each FTS table has a "rowid" column. The rowid of an FTS table behaves in the same way as the rowid column of an ordinary SQLite table, except that the values stored in the rowid column of an FTS table remain unchanged if the database is rebuilt using the VACUUM command. For FTS tables, "docid" is allowed as an alias along with the usual "rowid", "oid" and "oid" identifiers. Attempting to insert or update a row with a docid value that already exists in the table is an error, just as it would be with an ordinary SQLite table.

这意味着您可以使用内置的 docid 列作为您的主键,并让 fts 表对其应用约束。

关于android - 如何在不使用 CONFLICT_REPLACE 的情况下获得 "insert or update"行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13300689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com