Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Sqlite3 (not PDO!) transactions?

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

like image 738
mr_app Avatar asked Aug 15 '13 16:08

mr_app


2 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

like image 90
F-3000 Avatar answered Nov 19 '22 15:11

F-3000


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

like image 7
CL. Avatar answered Nov 19 '22 16:11

CL.