PHPackages                             kora/sqlite - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. kora/sqlite

ActiveLibrary[Database &amp; ORM](/categories/database)

kora/sqlite
===========

Sqlite for the Kora Framework.

0.1.1(1y ago)11MITPHPPHP ^7.4 || ^8.0

Since Jan 21Pushed 1y ago1 watchersCompare

[ Source](https://github.com/koraphp/sqlite)[ Packagist](https://packagist.org/packages/kora/sqlite)[ Docs](https://github.com/koraphp/sqlite)[ RSS](/packages/kora-sqlite/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

SQLiteDatabase Class
====================

[](#sqlitedatabase-class)

This provides a detailed overview of how to set up and use the **SQLiteDatabase** class. It covers installation, configuration, examples of common usage patterns, transaction handling, logging, and more. The goal is to help you quickly integrate and leverage this database wrapper in your own PHP 7.4+ projects.

Table of Contents
-----------------

[](#table-of-contents)

1. [Introduction](#introduction)
2. [Requirements](#requirements)
3. [Class Overview](#class-overview)
4. [Basic Usage](#basic-usage)
5. [Transaction Handling](#transaction-handling)
6. [Convenience Methods](#convenience-methods)
7. [Logging](#logging)
8. [Error Handling &amp; Exceptions](#error-handling--exceptions)

Introduction
------------

[](#introduction)

The **SQLiteDatabase** class is a robust, PSR-compatible wrapper around SQLite’s native PDO driver. It provides:

- **Secure prepared statements** to avoid SQL injection.
- **Lazy-loaded connection** so you only connect when you actually run queries.
- **Transaction helpers** like a dedicated `transaction()` method that automatically handles commits and rollbacks.
- **Convenience methods** (`fetchAll()`, `fetchOne()`, `fetchColumn()`, etc.) for simpler query operations.
- **Optional PSR-3 logging** (e.g., via Monolog), with info/error/debug levels for deeper insights.

This guide assumes you have basic knowledge of PHP, Composer, and how to configure a PSR-4 autoloader.

Requirements
------------

[](#requirements)

1. **PHP 7.4+** (strict typing is enforced via `declare(strict_types=1)`).
2. **SQLite PDO Extension** (usually included by default in most PHP distributions).
3. **PSR-4 Autoloader** if you plan to integrate this class into a larger application.
4. **(Optional) PSR-3 Logger** for logging, such as [Monolog](https://github.com/Seldaek/monolog).

Class Overview
--------------

[](#class-overview)

Below is a high-level outline of the main properties and methods you’ll interact with in `SQLiteDatabase`:

- **Properties**

    - `$databasePath` – the path to your SQLite database file.
    - `$logger` – a PSR-3 logger instance (optional).
    - `$connection` – holds the PDO connection (lazily instantiated).
- **Methods**

    1. `getConnection()` – Establishes or returns an existing PDO connection.
    2. `execute($query, $params = [])` – Executes a non-SELECT statement (INSERT, UPDATE, DELETE). Returns affected rows.
    3. `fetchOne($query, $params = [])` – Fetches a single row as an associative array.
    4. `fetchAll($query, $params = [])` – Fetches all rows as an array of associative arrays.
    5. `fetchColumn($query, $params = [], $columnIndex = 0)` – Fetches a single scalar value (e.g., `COUNT(*)`).
    6. `transaction(\Closure $callback)` – Wraps operations in a transaction, auto-committing or rolling back.
    7. `tableExists($tableName)` – Checks if a table exists in the database.
    8. `getLastInsertId()` – Retrieves the ID of the last inserted row.
    9. `close()` – Closes the database connection.

Basic Usage
-----------

[](#basic-usage)

A common workflow is to instantiate `SQLiteDatabase` once and then call its query methods:

```
