Databases for C multi-tenant AI Apps.
Turso · Docs · Quickstart · SDK Reference · Blog & Tutorials
- 🔌 Works offline with Embedded Replicas
- 🌎 Works with remote Turso databases
- ✨ Works with Turso AI & Vector Search
Warning
This SDK is currently in technical preview, and mostly used for internal use when building other libSQL SDKs. Join us in Discord to report any issues.
-
Clone the repository:
git clone https://github.com/your-repo/libsql-c.git cd libsql-c
-
Build the library:
cargo build --release
-
The compiled library will be in
target/release/
:liblibsql.so
(Linux)liblibsql.dylib
(macOS)liblibsql.dll
(Windows)
-
Copy
libsql.h
and the compiled library to your project directory or a standard system location.
-
Write your program:
#include <stdio.h> #include "libsql.h" int main() { libsql_setup((libsql_config_t){0}); libsql_database_t db = libsql_database_init((libsql_database_desc_t){ .path = "local.db" }); if (db.err) { fprintf(stderr, "Error: %s\n", libsql_error_message(db.err)); return 1; } libsql_connection_t conn = libsql_database_connect(db); if (conn.err) { fprintf(stderr, "Connection error: %s\n", libsql_error_message(conn.err)); return 1; } const char* sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT);" "INSERT INTO users (name) VALUES ('Alice');"; libsql_batch_t batch = libsql_connection_batch(conn, sql); if (batch.err) { fprintf(stderr, "Batch error: %s\n", libsql_error_message(batch.err)); return 1; } printf("Database operations completed successfully.\n"); libsql_connection_deinit(conn); libsql_database_deinit(db); return 0; }
-
Compile your program, linking against the libsql library:
gcc -o example example.c -L/path/to/libsql -llibsql
-
Run your program:
./example
Example | Description |
---|---|
local | Uses libsql with a local SQLite file. Creates database, inserts data, and queries. |
remote | Connects to a remote database. Requires environment variables for URL and auth token. |
sync | Demonstrates synchronization between local and remote databases. |
batch | Executes multiple SQL statements in a single batch operation. |
transactions | Shows transaction usage: starting, performing operations, and committing/rolling back. |
memory | Uses an in-memory SQLite database for temporary storage or fast access. |
vector | Works with vector embeddings, storing and querying for similarity search. |
encryption | Creates and uses an encrypted SQLite database, demonstrating setup and data operations. |
Visit our official documentation.
Join us on Discord to get help using this SDK. Report security issues via email.
See the contributing guide to learn how to get involved.