Skip to content

tursodatabase/libsql-c

Repository files navigation

libSQL C

libSQL C

Databases for C multi-tenant AI Apps.

Turso · Docs · Quickstart · SDK Reference · Blog & Tutorials

MIT License Discord Contributors Total downloads Examples

Features

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.

Install

  1. Clone the repository:

    git clone https://github.com/your-repo/libsql-c.git
    cd libsql-c
  2. Build the library:

    cargo build --release
  3. The compiled library will be in target/release/:

    • liblibsql.so (Linux)
    • liblibsql.dylib (macOS)
    • liblibsql.dll (Windows)
  4. Copy libsql.h and the compiled library to your project directory or a standard system location.

Quickstart

  1. 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;
    }
  2. Compile your program, linking against the libsql library:

    gcc -o example example.c -L/path/to/libsql -llibsql
    
  3. Run your program:

    ./example
    

Examples

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.

Documentation

Visit our official documentation.

Support

Join us on Discord to get help using this SDK. Report security issues via email.

Contributors

See the contributing guide to learn how to get involved.

Contributors

good first issue