https://github.com/tursodatabase/libsql-c
libSQL bindings for C
https://github.com/tursodatabase/libsql-c
Last synced: about 2 months ago
JSON representation
libSQL bindings for C
- Host: GitHub
- URL: https://github.com/tursodatabase/libsql-c
- Owner: tursodatabase
- License: mit
- Created: 2024-09-08T02:21:24.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-10-23T23:32:34.000Z (8 months ago)
- Last Synced: 2024-10-24T13:05:27.771Z (8 months ago)
- Language: Rust
- Size: 346 KB
- Stars: 7
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
libSQL C
Databases for C multi-tenant AI Apps.
Turso ·
Docs ·
Quickstart ·
SDK Reference ·
Blog & Tutorials## Features
- 🔌 Works offline with [Embedded Replicas](https://docs.turso.tech/features/embedded-replicas/introduction)
- 🌎 Works with remote Turso databases
- ✨ Works with Turso [AI & Vector Search](https://docs.turso.tech/features/ai-and-embeddings)> [!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:
```bash
git clone https://github.com/your-repo/libsql-c.git
cd libsql-c
```2. Build the library:
```bash
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:
```c
#include
#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](examples/local) | Uses libsql with a local SQLite file. Creates database, inserts data, and queries. |
| [remote](examples/remote) | Connects to a remote database. Requires environment variables for URL and auth token. |
| [sync](examples/sync) | Demonstrates synchronization between local and remote databases. |
| [batch](examples/batch) | Executes multiple SQL statements in a single batch operation. |
| [transactions](examples/transactions) | Shows transaction usage: starting, performing operations, and committing/rolling back. |
| [memory](examples/memory) | Uses an in-memory SQLite database for temporary storage or fast access. |
| [vector](examples/vector) | Works with vector embeddings, storing and querying for similarity search. |
| [encryption](examples/encryption) | Creates and uses an encrypted SQLite database, demonstrating setup and data operations. |## Documentation
Visit our [official documentation](https://docs.turso.tech/sdk/c).
## Support
Join us [on Discord](https://tur.so/discord-c) to get help using this SDK. Report security issues [via email](mailto:[email protected]).
## Contributors
See the [contributing guide](CONTRIBUTING.md) to learn how to get involved.
