# Swift Reference

> libSQL Swift Reference
> Canonical HTML: https://tursodb.cn/docs/sdk/swift/reference/
> Upstream Source (MIT): https://docs.turso.tech/sdk/swift/reference
> Language: en

---
<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

This is currently in technical preview. [Join us in Discord](https://tur.so/discord) to report any issues.

</div></div>

## Installing

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

<div class="doc-cards">
<a class="doc-card" href="https://github.com/tursodatabase/libsql-swift" target="_blank" rel="noopener noreferrer"><div class="doc-card-title"><span>libSQL Swift</span><span class="doc-card-arrow" aria-hidden="true">↗</span></div><p class="doc-card-desc">Build from source code</p></a>
</div>


Or add it to your SwiftPM dependencies:

```swift
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](https://www.sqlite.org/inmemorydb.html) for cases where you don't
require persistence:

```swift
import Libsql

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

## Local Development

You can work locally using an SQLite file:

```swift
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:

```swift
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:

```swift
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:

```swift
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:

```swift
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:

```swift Execute
try conn.execute("INSERT INTO users VALUES (?)", [1])
```

```swift Query
try conn.query("SELECT * FROM users")
```

```swift Arguments
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()`:

```swift
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:

```swift Positional
try conn.query("SELECT * FROM users WHERE id = ?", [1])
```

```swift Named
try conn.query("SELECT * FROM users WHERE id = :id", [ ":id": 1 ])
```
