This is currently in technical preview. Join us in Discord to report any issues.
- Retrieve database credentials
- Install the libSQL package
- Connect to a local or remote Turso database
- Execute a query using SQL
- Sync changes to local database (optional)
1
Retrieve database credentials
You will need an existing database to continue. If you don’t have one, create one.Get the database URL:Get the database authentication token:Assign credentials to the environment variables inside
.env
.You will want to make sure to handle database tokens securely.
2
Install
First begin by adding Or add it to your SwiftPM dependencies:
libsql
as a package dependency in XCode using this repo:libSQL Swift
Build from source code
3
Connect
You must first create a
Database
object and then open a Connection
to it:Embedded Replicas
Embedded Replicas
Local only
Local only
Remote only
Remote only
4
Execute
You can execute a SQL query against your existing database by calling If you need to use placeholders for values, you can do that:
query()
,
or execute()
when you expect the query to not yield any rows:5
Sync (Embedded Replicas only)
When using embedded replicas you should call
sync()
on the database type to
sync your local database with the primary database, unless you are using
sync_interval
(though there is no issue with calling sync
with
sync_interval
enabled):