High-level helpers for SQLite3 with batteries-included query utilities, schema introspection, and pragma management.
- PHP 8.2 or newer
ext-sqlite3andext-mbstring
composer require r4ndsen/sqlite-phpgit clone https://github.com/r4ndsen/sqlite-php.git
cd sqlite-php
composer install<?php
use r4ndsen\SQLite;
use r4ndsen\SQLite\Column;
$sqlite = new SQLite(':memory:');
$tasks = $sqlite->getTable('tasks');
$tasks
->addCreateColumn(Column::createIntegerColumn('id'))
->addCreateColumn(Column::createTextColumn('title')->disallowNull())
->addCreateColumn(Column::createIntegerColumn('is_done', 0))
->createIfNotExists();
$tasks->getDynamicInsertTable()
->push(['id' => 1, 'title' => 'Write docs'])
->push(['id' => 2, 'title' => 'Tag release', 'is_done' => 1])
->commit();
$openTasks = $sqlite->fetchPairs(
'select id, title from tasks where is_done = :is_done order by id',
['is_done' => 0],
);
// [1 => 'Write docs']
foreach ($tasks as $row) {
// Table implements IteratorAggregate, so you can iterate rows directly
}
$sqlite->validate(); // Runs PRAGMA integrity_check and throws on corruptionHead over to the documentation for more detail on tables, column helpers, pragmas, and query convenience methods.
All just recipes run from the project root (install just from https://github.com/casey/just):
just test # PHPUnit against the current PHP version
just tests # PHPUnit against the Docker matrix (PHP 8.2–8.5)
just coverage # Generates HTML coverage and prints the report path
just infection # Mutation testing (requires Xdebug)
just stan # PHPStan static analysisr4ndsen/sqlite-php is licensed under the MIT License. See LICENSE for details.
See CHANGELOG for release notes.