我使用的是sqlite 3.7,其架构如下:
CREATE TABLE IF NOT EXISTS subjects (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
);
CREATE TABLE IF NOT EXISTS predicates (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
predicate TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS objects (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
object BLOB NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS statements (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
subject INTEGER NOT NULL REFERENCES subjects(id) ON DELETE RESTRICT,
predicate INTEGER NOT NULL REFERENCES predicates(id) ON DELETE RESTRICT,
object INTEGER NULL REFERENCES objects(id) ON DELETE RESTRICT
);
要在statements
中插入一行,我首先插入或忽略predicate
和object
,然后检索ID:
INSERT OR IGNORE INTO predicates(predicate) VALUES (?)
SELECT predicates.id FROM predicates WHERE predicates.predicate = ?
最终执行
INSERT INTO statements(subject, predicate, object) VALUES (?, ?, ?)
我的问题是:有没有办法一次性直接执行这些(准备好的)语句?
INSERT OR IGNORE INTO predicates(predicate) VALUES (?);
SELECT predicates.id FROM predicates WHERE predicates.predicate = ?;
INSERT OR IGNORE INTO objects(object) VALUES (?);
SELECT predicates.id FROM objects WHERE objects.object = ?;
INSERT INTO statements(subject, predicate, object) VALUES (?, ?, ?);
答案 0 :(得分:1)
last_insert_rowid()仅在实际插入某行时才有效。
您总是可能需要SELECT,但您可以将它们作为子查询来执行:
INSERT OR IGNORE INTO predicates(predicate) VALUES (?);
INSERT OR IGNORE INTO objects(object) VALUES (?);
INSERT INTO statements(subject, predicate, object)
VALUES (?,
(SELECT id FROM predicates WHERE predicate = ?),
(SELECT id FROM objects WHERE object = ?)
);