该视图需要一个名为 rowid 的列。身份证不够用。注意:SQLite 网站上的外部内容表示例具有误导性:
INSERT INTO t3(docid, b, c) SELECT id, b, c FROM t2;
使用 and id 列适用于普通表,但不适用于视图。您需要改用 rowid:
INSERT INTO t3(docid, b, c) SELECT id rowid, b, c FROM t2;
基于 SQLite 文档示例的带有 SQLite 3.11 的 Linux 的完整控制台示例。
使用视图不起作用:
$ sqlite3 does_not_work.db;
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> CREATE TABLE t2(id INTEGER PRIMARY KEY, a, b, c);
sqlite> CREATE VIEW t2_v AS SELECT id, a, b, c FROM t2;
sqlite> CREATE VIRTUAL TABLE t3 USING fts4(content="t2_v", b, c);
sqlite> INSERT INTO t2 VALUES(2, 'a b', 'c d', 'e f');
sqlite> INSERT INTO t2 VALUES(3, 'g h', 'i j', 'k l');
sqlite> INSERT INTO t3(docid, b, c) SELECT id, b, c FROM t2_v;
sqlite> SELECT * FROM t3 WHERE t3 MATCH 'k';
|
sqlite> .exit
但是在创建视图时为 id 设置 rowid 的列别名是可行的:
sqlite3 works.db;
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> CREATE TABLE t2(id INTEGER PRIMARY KEY, a, b, c);
sqlite> CREATE VIEW t2_v AS SELECT id rowid, a, b, c FROM t2;
sqlite> CREATE VIRTUAL TABLE t3 USING fts4(content="t2_v", b, c);
sqlite> INSERT INTO t2 VALUES(2, 'a b', 'c d', 'e f');
sqlite> INSERT INTO t2 VALUES(3, 'g h', 'i j', 'k l');
sqlite> INSERT INTO t3(docid, b, c) SELECT rowid, b, c FROM t2_v;
sqlite> SELECT * FROM t3 WHERE t3 MATCH 'k';
i j|k l
sqlite>
使用普通表虽然有效:
$ sqlite3 also_works.db;
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> CREATE TABLE t2(id INTEGER PRIMARY KEY, a, b, c);
sqlite> CREATE VIRTUAL TABLE t3 USING fts4(content="t2", b, c);
sqlite> INSERT INTO t2 VALUES(2, 'a b', 'c d', 'e f');
sqlite> INSERT INTO t2 VALUES(3, 'g h', 'i j', 'k l');
sqlite> INSERT INTO t3(docid, b, c) SELECT id, b, c FROM t2;
sqlite> SELECT * FROM t3 WHERE t3 MATCH 'k';
i j|k l
sqlite>