Turso Quickstart (Rust)
Get started with Turso and Rust using the libSQL crate in a few simple steps
In this Rust quickstart we will learn how to:
- Retrieve database credentials
- Install the Rust libSQL crate
- Connect to a local or remote Turso database
- Execute a query using SQL
- Sync changes to local database (optional)
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
.
Install
First begin by installing the libsql
crate:
Connect
You must first create a Database
object and then open a Connection
to it:
Execute
You can execute a SQL query against your existing database by calling execute()
:
If you need to use placeholders for values, you can do that:
To retrieve results from a query, you can use the query()
method:
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:
You can also set up automatic periodic syncing when creating the database:
This will automatically sync the database every 60 seconds.