Home » Php » sqlite – PHP Sqlite3 (not PDO!) transactions?

sqlite – PHP Sqlite3 (not PDO!) transactions?

Posted by: admin July 12, 2020 Leave a comment

Questions:

Is it possible to use transactions (and rollbacks) with sqlite3 drivers in PHP? I havn’t found infos here: http://de2.php.net/manual/en/book.sqlite3.php

I dont want to use PDO…

Thanks for your help

How to&Answers:

Yes, transactions and rollbacks are possible even without PDO. Astonishing how difficult it is to find an example that explains how to accomplish this. Had to actually dig into some ready code to find out.

$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;');

Source for logic: sombra2eternity, source for ‘MyDB’: php.net

Answer:

Just execute the appropriate SQL commands: BEGIN/COMMIT/ROLLBACK.