我在 PHP-PDO-SQLite 中的事务和更新有问题。
$db = new PDO('sqlite:database1.sqlite');
/*
$rowsnumber1 = $db->exec("CREATE TABLE IF NOT EXISTS questions(
id INTEGER PRIMARY KEY AUTOINCREMENT,
question TEXT NOT NULL,
answers INTEGER NOT NULL
)");
print('$rowsnumber1: '.$rowsnumber1.'<br />');
$rowsnumber2 = $db->exec("CREATE TABLE IF NOT EXISTS answers(
id INTEGER PRIMARY KEY AUTOINCREMENT,
qid INTEGER NOT NULL,
answer TEXT NOT NULL
)");
print('$rowsnumber2: '.$rowsnumber2.'<br />');
*/
//print('Inserting: '); $res = $db->exec("INSERT INTO questions (question,answers) VALUES ('Question',0)"); var_dump($res); print('<br />');
$qid = 1;
try
{
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
// Variant 1
//print('Executing 1: '); $res1 = $db->exec("INSERT INTO answers (qid,answer) VALUES ($qid,'Answer')"); var_dump($res1); print('<br />');
//print('Executing 2: '); $res2 = $db->exec("UPDATE questions SET answers = answers+1 WHERE id = '".$qid."'"); var_dump($res2); print('<br />');
// Variant 2
print('Preparing 1: '); $statement1 = $db->prepare("INSERT INTO answers (qid,answer) VALUES (:qid,:answer)"); var_dump($statement1); print('<br />');
print('Preparing 2: '); $statement2 = $db->prepare("UPDATE questions SET answers = answers+1 WHERE id='".$qid."'"); var_dump($statement2); print('<br />');
print('Executing 1: '); $res1 = $statement1->execute(array('qid'=>$qid,'answer'=>'Answer')); var_dump($res1); print('<br />');
print('Executing 2: '); $res2 = $statement2->execute(); var_dump($res2); print('<br />');
$db->commit();
}
catch(Exception $e)
{
$db->rollBack();
print("Transaction failed: " .$e->getMessage());
}
当问题的“id”和答案的“qid”相同($qid)时,没有问题。当我尝试插入带有问题“id”的答案时,问题就开始了,该问题在“问题”表中不存在。例如,在“问题”表中有一个问题(“id”为“1”),您尝试用“id”=“5”插入问题的答案。答案被插入,但问题没有更新(单元格“答案”没有增加),因为没有这样的“id”=“5”的问题。问题是事务不会进行回滚,并且执行更新时结果为“true”,尽管实际上没有更新。交易有什么问题以及为什么更新返回“true”