我在 PHP 中使用 sqlite_last_insert_rowid 时遇到问题。代码:
$database = new SQLiteDatabase('example.db');
$sql_1 = "INSERT INTO ex1(test1, test2) VALUES ('$test1','$test2')";
$database->queryExec($sql_1);
$ex1_ID = sqlite_last_insert_rowid($database);
$sql_2 = "INSERT INTO ex2 (ex1_ID, fname, lname) values ('$ex1_ID','$fname','$lname');";
$database->queryExec($sql);
我想将 ex1 表中的 ID 存储到 ex1_ID 中的表 ex2
CREATE TABLE ex1 (
ID INTEGER PRIMARY KEY,
test1 LONGTEXT,
test2 LONGTEXT
);
CREATE TABLE ex2 (
ID INTEGER PRIMARY KEY,
ex1_ID INTEGER,
fname LONGTEXT,
lname LONGTEXT,
FOREIGN KEY ( ex1_ID ) REFERENCES ex1 ( ID )
);
完整代码:
$database = new SQLiteDatabase('example.db');
for ($i = 0; $i < 10; $i++) {
$test1 = "";
$test2 = "";
$lenght = rand(300, 400);
for ($x = 0; $x < $lenght; $x++) {
$test1 .= rand(0, 9);
$test2 .= rand(0, 9);
}
$sql_1 = "INSERT INTO ex1 (test1,test2) VALUES ('$test1','$test2')";
$database->queryExec($sql_1);
$ex1_ID = sqlite_last_insert_rowid($database);
for ($j = 0; $j < 3; $j++) {
for ($x = 0; $x < $lenght; $x++) {
$test1 .= rand(0, 9);
$test2 .= rand(0, 9);
}
$sql_2 = "INSERT INTO ex2 (ex1_ID, fname, lname) values ('$ex1_ID','$test1','$test2');";
$database->queryExec($sql_2);
}
}
我无法从 ex1 表中获取最后一个 ID。请帮忙!