PHP Sqlite3(不是PDO!)事务?
在PHP中是否可以将事务(和回滚)与sqlite3驱动程序一起使用?我在这里找不到信息: 我不想使用PDOPHP Sqlite3(不是PDO!)事务?,php,sqlite,transactions,rollback,Php,Sqlite,Transactions,Rollback,在PHP中是否可以将事务(和回滚)与sqlite3驱动程序一起使用?我在这里找不到信息: 我不想使用PDO 感谢您的帮助只需执行适当的操作。是的,即使没有PDO,也可以执行事务和回滚。令人惊讶的是,找到一个例子来解释如何做到这一点是多么困难。必须深入研究一些现成的代码才能找到答案 $db=new MyDB("database.db", SQLITE3_OPEN_READWRITE); $db->exec('BEGIN;'); $stmt=$db->prepare('UPDATE
感谢您的帮助只需执行适当的操作。是的,即使没有PDO,也可以执行事务和回滚。令人惊讶的是,找到一个例子来解释如何做到这一点是多么困难。必须深入研究一些现成的代码才能找到答案
$db=new MyDB("database.db", SQLITE3_OPEN_READWRITE);
$db->exec('BEGIN;');
$stmt=$db->prepare('UPDATE table SET name = :name WHERE id = :id');
$stmt->bindValue(':id', $id, SQLITE3_INTEGER);
$stmt->bindValue(':name', $name, SQLITE3_TEXT);
$stmt->execute();
$db->exec('COMMIT;');
逻辑源:,“MyDB”源:对上述F-3000答案的简短扩展。如果创建自己的类,也可以编写自己的包装函数。例如:
class MyDB extends SQLite3 {
public function __construct(string $filename, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, string $encryptionKey = '') {
parent::__construct($filename, $flags, $encryptionKey);
$this->exec('PRAGMA busy_timeout = 9900');
$this->exec('PRAGMA encoding = "UTF-8"');
// more pragmas ...
}
public function enum(string $table, string $key, string $value) {
$enum = array();
$sql = "select distinct $table.$key, $table.$value
from $table
where $table.$key is not null";
if ($key !== $value) {
$sql .= "
and $table.$value is not null";
}
$sql .= "
order by null";
$result = $this->query($sql);
if ($result !== false) {
$row = $result->fetchArray(SQLITE3_NUM);
while ($row !== false) {
$enum[$row[0]] = $row[1];
$row = $result->fetchArray(SQLITE3_NUM);
}
}
return $enum;
}
public function begin() {
$this->exec('BEGIN');
}
public function commit() {
$this->exec('COMMIT');
}
public function rollback() {
$this->exec('ROLLBACK');
}
// more functions ...
} // MyDB //
困难的部分是何时执行imo。在这方面有很大的错误空间。我还建议用
catch
中的rollback
将其包装在try
catch
中。