splitbrain / php-sqlite
A simple wrapper around pdo-sqlite with basic migration support and a few helper utils
1.1.1
2025-03-22 20:57 UTC
Requires
- php: >=7.4
- ext-pdo: *
- ext-pdo_sqlite: *
Requires (Dev)
- phpunit/phpunit: ^8.0
README
A lightweight PHP library for working with SQLite databases with automatic schema migrations.
This does not replace a full ORM like Doctrine, but it's also like a million times lighter.
Features
- Simple helpers for common SQLite operations
- Automatic schema migrations
- Prepared statement handling
- Enables WAL journal mode, foreign key support and exceptions by default
Installation
composer require splitbrain/php-sqlite
Basic Usage
use splitbrain\phpsqlite\SQLite; // Initialize with database file and schema directory $db = new SQLite('path/to/database.sqlite', 'path/to/migrations'); // Apply any pending migrations $db->migrate(); // Query data $contacts = $db->queryAll("SELECT * FROM contacts WHERE name LIKE ?", "%John%"); // Get a single record $contact = $db->queryRecord("SELECT * FROM contacts WHERE contact_id = ?", 42); // Get a single value $count = $db->queryValue("SELECT COUNT(*) FROM contacts"); // Insert or update data $newContact = $db->saveRecord("contacts", [ "name" => "John Doe", "email" => "john@example.com" ]); // Execute statements $db->exec("DELETE FROM contacts WHERE contact_id = ?", 42);
Check the SQLite class for more methods.
Schema Migrations
Place your migration files in the schema directory with filenames like:
0001.sql
- Initial schema0002.sql
- Add new tables0003.sql
- Add sample data
The library will automatically apply migrations in order when you call $db->migrate()
.
A table called opts
is automatically created to track applied migrations. You can also use this table to store
configuration values.