soluzione di Herman ha lavorato per me, ma il ...
mi aveva confuso per un po '. Sto includendo la demo che ho elaborato in base alla sua risposta. Le funzionalità aggiuntive nella mia risposta includono il supporto per le chiavi esterne, i tasti di incremento automatico e l'uso della funzione last_insert_rowid()
per ottenere l'ultima chiave generata automaticamente in una transazione.
mio bisogno di queste informazioni è venuto quando mi ha colpito una transazione che ha richiesto tre chiavi esterne, ma ho potuto ottenere solo l'ultimo con last_insert_rowid()
.
PRAGMA foreign_keys = ON; -- sqlite foreign key support is off by default
PRAGMA temp_store = 2; -- store temp table in memory, not on disk
CREATE TABLE Foo(
Thing1 INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
);
CREATE TABLE Bar(
Thing2 INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
FOREIGN KEY(Thing2) REFERENCES Foo(Thing1)
);
BEGIN TRANSACTION;
CREATE TEMP TABLE _Variables(Key TEXT, Value INTEGER);
INSERT INTO Foo(Thing1)
VALUES(2);
INSERT INTO _Variables(Key, Value)
VALUES('FooThing', last_insert_rowid());
INSERT INTO Bar(Thing2)
VALUES((SELECT Value FROM _Variables WHERE Key = 'FooThing'));
DROP TABLE _Variables;
END TRANSACTION;
sqlite non supporta questo. –