https://github.com/phenixphp/sqlite
Async SQLite 3 implementation using Amphp V3.
https://github.com/phenixphp/sqlite
Last synced: 21 days ago
JSON representation
Async SQLite 3 implementation using Amphp V3.
- Host: GitHub
- URL: https://github.com/phenixphp/sqlite
- Owner: phenixphp
- License: other
- Created: 2026-01-29T16:48:12.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2026-01-31T21:22:04.000Z (about 2 months ago)
- Last Synced: 2026-02-01T08:27:18.895Z (about 2 months ago)
- Language: PHP
- Size: 82 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# phenixphp/sqlite
Asynchronous SQLite 3 client for PHP based on [Amp](https://amphp.org/).
[](https://github.com/phenixphp/sqlite/actions/workflows/run-tests.yml)
[](https://packagist.org/packages/phenixphp/sqlite)
[](https://packagist.org/packages/phenixphp/sqlite)
[](https://packagist.org/packages/phenixphp/sqlite)
[](https://packagist.org/packages/phenixphp/sqlite)
---
**phenixphp/sqlite** is part of the **Phenix PHP** framework ecosystem. Phenix is a web framework built on pure PHP, without external extensions, based on the [Amphp](https://amphp.org/) ecosystem, which provides non-blocking operations, asynchronism and parallel code execution natively. It runs in the PHP SAPI CLI and on its own server — it is simply powerful.
---
## Table of Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Establishing a Connection](#establishing-a-connection)
- [Executing Queries](#executing-queries)
- [Prepared Statements](#prepared-statements)
- [Transactions](#transactions)
- [License](#license)
---
## Requirements
| Requirement | Version |
| --- | --- |
| PHP | `^8.2` |
| ext-sqlite3 | `*` |
---
## Installation
Install the package via [Composer](https://getcomposer.org/):
```bash
composer require phenixphp/sqlite
```
---
## Configuration
The `SqliteConfig` class is the entry point for configuring the SQLite connection. It allows you to define the database path and connection parameters aligned with the Amphp SQL ecosystem.
```php
You can use `:memory:` as the database path to create an in-memory SQLite database, which is ideal for testing scenarios.
```php
$config = new SqliteConfig(
database: ':memory:',
);
```
---
## Usage
### Establishing a Connection
Use `SqliteConfig` to create and manage asynchronous connections to your SQLite database. Since this package is built on top of Amphp, all database operations are **non-blocking** and run asynchronously using fibers.
```php
close();
```
---
### Executing Queries
Once you have an active connection, you can execute SQL statements — `CREATE`, `INSERT`, `SELECT`, `UPDATE`, and `DELETE` — all asynchronously without blocking the event loop.
#### Creating a Table
```php
$connection->execute(
'CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL
)'
);
```
#### Inserting Records
```php
$connection->execute(
"INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')"
);
```
#### Selecting Records
```php
$result = $connection->query('SELECT * FROM users');
foreach ($result as $row) {
echo $row['id'] . ': ' . $row['name'] . ' — ' . $row['email'] . PHP_EOL;
}
```
#### Updating Records
```php
$connection->execute(
"UPDATE users SET name = 'Jane Doe' WHERE id = 1"
);
```
#### Deleting Records
```php
$connection->execute(
"DELETE FROM users WHERE id = 1"
);
```
---
### Prepared Statements
Prepared statements allow you to precompile a SQL template and execute it repeatedly with different bound parameters. This improves both performance and security by preventing SQL injection.
#### Preparing and Executing a Statement
```php
$statement = $connection->prepare(
'INSERT INTO users (name, email) VALUES (?, ?)'
);
$statement->execute(['Alice', 'alice@example.com']);
$statement->execute(['Bob', 'bob@example.com']);
```
#### Prepared Statement with Named Parameters
```php
$statement = $connection->prepare(
'SELECT * FROM users WHERE name = ? AND email = ?'
);
$result = $statement->execute(['Alice', 'alice@example.com']);
foreach ($result as $row) {
echo $row['name'] . PHP_EOL;
}
```
#### Closing a Prepared Statement
```php
$statement->close();
```
---
### Transactions
Transactions group multiple SQL operations into a single atomic unit. If any operation fails, all changes within the transaction are rolled back, ensuring data consistency.
#### Basic Transaction
```php
$transaction = $connection->beginTransaction();
try {
$transaction->execute(
"INSERT INTO users (name, email) VALUES ('Charlie', 'charlie@example.com')"
);
$transaction->execute(
"INSERT INTO users (name, email) VALUES ('Diana', 'diana@example.com')"
);
$transaction->commit();
} catch (\Throwable $e) {
$transaction->rollback();
throw $e;
}
```
#### Transaction with Prepared Statements
```php
$transaction = $connection->beginTransaction();
try {
$statement = $transaction->prepare(
'INSERT INTO users (name, email) VALUES (?, ?)'
);
$statement->execute(['Eve', 'eve@example.com']);
$statement->execute(['Frank', 'frank@example.com']);
$transaction->commit();
} catch (\Throwable $e) {
$transaction->rollback();
throw $e;
}
```
---
## Learning Phenix
You can learn about Phenix in the official [documentation](https://phenix.omarbarbosa.com) and discover the power of asynchronous and concurrent applications in native PHP.
## Security Vulnerabilities
If you discover a security vulnerability within Phenix, please send an e-mail to Omar Barbosa via [contacto@omarbarbosa.com](mailto:contacto@omarbarbosa.com). All security vulnerabilities will be promptly addressed.
## License
This package is open-sourced software licensed under the [MIT](https://opensource.org/licenses/MIT) license.