Skip to main content
Multilingual TursoDB, MVCC & AI Agent database guides Read Engine Guide →
Turso Cloud SDKs Official SDKs Swift

Swift Reference

libSQL Swift Reference

WARNING

This is currently in technical preview. Join us in Discord to report any issues.

Installing

First begin by adding libsql as a package dependency in XCode using this repo:

Or add it to your SwiftPM dependencies:

import PackageDescription

let package = Package(
    // ...
    dependencies: [
        .package(url: "https://github.com/tursodatabase/libsql-swift", from: "0.1.1"),
    ],
    // ...
)

In-Memory Databases

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

import Libsql

let db = Database(":memory:")
let conn = try db.connect()

Local Development

You can work locally using an SQLite file:

import Libsql

let db = Database("local.db")
let conn = try db.connect()

Embedded Replicas

You can work with embedded replicas that can sync from the remote URL and delegate writes to the remote primary database:

import Libsql

let db = try Database(
    path: "./local.db",
    url: "TURSO_DATABASE_URL",
    authToken: "TURSO_AUTH_TOKEN"
)

let conn = try db.connect()

Manual Sync

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

try db.sync() // Call sync manually to update local database

Sync Interval

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

import Libsql

let db = try Database(
    path: "./local.db",
    url: "TURSO_DATABASE_URL",
    authToken: "TURSO_AUTH_TOKEN"
    syncInterval: 300 // Sync every 3 seconds
)

let conn = try db.connect()

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 to the function:

import Libsql

let db = try Database(
    path: "./local.db",
    url: <LIBSQL_URL>,
    authToken: <LIBSQL_AUTH_TOKEN>,
    readYourWrites: false
)

let conn = try db.connect()

Simple query

You can pass a string to query() to invoke a SQL statement, as well as optional arguments:

try conn.execute("INSERT INTO users VALUES (?)", [1])
try conn.query("SELECT * FROM users")
try conn.query("SELECT * FROM users WHERE id = ?", [1])

Prepared Statements

You can prepare a cached statement using prepare() and then execute it with query():

let stmt = try conn.prepare("SELECT * FROM users WHERE id = ?")
stmt.bind([1])
stmt.query()

Placeholders

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

try conn.query("SELECT * FROM users WHERE id = ?", [1])
try conn.query("SELECT * FROM users WHERE id = :id", [ ":id": 1 ])
Synced from official Turso documentation ( tursodatabase/turso-docs · MIT License) Official Docs ↗
Search
Press Esc to close · Powered by Pagefind