Skip to main content

Installing

Install the package to your project using composer:

Initializing

Make sure to add use Libsql\Database to access the Database object.

In-Memory Databases

libSQL supports connecting to in-memory databases for cases where you don’t require persistence:
Or the simpler:

Local Development

You can work locally by passing a path as the first parameter.
Or more explicitly:

Remote Only

You can use a remote only database by passing url and authToken.

Embedded Replicas

You can work with embedded replicas by passing a path, url and authToken. Embedded replicas can sync from the remote URL and delegate writes to the remote primary database:

Sync Interval

The sync_interval function allows you to set an interval for automatic synchronization of the database in the background:

Manual Sync

The sync function allows you to sync manually the local database with the remote counterpart:

Read Your Own Writes

The readYourWrites parameter configures the database connection to ensure that writes are immediately visible to subsequent read operations initiated by the same connection. This is enabled by default, and is particularly important in distributed systems to ensure consistency from the perspective of the writing process. You can disable this behavior by passing false:

Simple Query

You can acquire a connection from a database and call query() to invoke a SQL statement, as well as optional arguments:

Prepared Statements

You can prepare a cached statement using prepare(), bind parameters, and then query it:

Placeholders

libSQL supports the use of positional and named placeholders within SQL statements:

Batch Transactions

A batch consists of multiple SQL statements executed sequentially within an implicit transaction. The backend handles the transaction: success commits all changes, while any failure results in a full rollback with no modifications.

Interactive Transactions

Interactive transactions in SQLite ensure the consistency of a series of read and write operations within a transaction’s scope. These transactions give you control over when to commit or roll back changes, isolating them from other client activity.