Skip to content

Commit

Permalink
examples: update readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
notrab committed Oct 10, 2024
1 parent 44c4fb6 commit be4e876
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 12 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
## Examples
- [Local](/examples/local)
- [Remote](/examples/remote)
- [Sync](/examples/sync)
- [Vector](/examples/vector)
- [Transactions](/examples/transactions)
- [Batch](/examples/batch)
| Example | Description |
| ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [local](examples/local) | Demonstrates how to use libsql with a local SQLite database file. Creates a database, inserts data, and performs queries. |
| [remote](examples/remote) | Shows how to connect to a remote database using libsql. Requires setting up environment variables for the database URL and authentication token. |
| [sync](examples/sync) | Illustrates the use of libsql's synchronization features. Creates a local database that syncs with a remote database, demonstrating how to handle offline and online operations. |
| [batch](examples/batch) | Demonstrates how to execute multiple SQL statements in a single batch operation using libsql. Useful for efficient execution of multiple related operations. |
| [transactions](examples/transactions) | Shows how to use transactions in libsql. Demonstrates starting a transaction, performing multiple operations, and committing or rolling back changes. |
| [memory](examples/memory) | Illustrates the use of an in-memory SQLite database with libsql. Useful for temporary storage, testing, or scenarios requiring fast access without data persistence. |
| [vector](examples/vector) | Demonstrates how to work with vector embeddings in libsql, including storing and querying vector data for similarity search. |
| [encryption](examples/encryption) | Demonstrates how to create and use an encrypted SQLite database with libsql. Shows setting up encryption, writing data, and reading from an encrypted database. |
## Documentation
Expand Down
2 changes: 1 addition & 1 deletion examples/batch/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Batch

This example demonstrates how to use libsql to execute a batch of SQL statements.
This example demonstrates how to use libSQL to execute a batch of SQL statements.

## Building

Expand Down
1 change: 1 addition & 0 deletions examples/encryption/Makefile
22 changes: 22 additions & 0 deletions examples/encryption/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Encryption

This example demonstrates how to create and use an encrypted SQLite database with libSQL.

## Building

```bash
make
```

## Running

```bash
./example
```

This example will:

1. Create an encrypted SQLite database file named `encrypted.db`.
2. Create a table called `secrets`.
3. Insert some sample data into the `secrets` table.
4. Query and display all secrets in the table.
99 changes: 99 additions & 0 deletions examples/encryption/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "../../libsql.h"
#include <stdio.h>

int main() {
libsql_setup((libsql_config_t){0});

const char *encryption_key = "my_secret_key";

libsql_database_t db = libsql_database_init((libsql_database_desc_t
){.path = "encrypted.db",
.encryption_key = encryption_key,
.cypher = LIBSQL_CYPHER_AES256});

if (db.err) {
fprintf(
stderr,
"Error initializing encrypted database: %s\n",
libsql_error_message(db.err)
);
return 1;
}

libsql_connection_t conn = libsql_database_connect(db);
if (conn.err) {
fprintf(
stderr,
"Error connecting to encrypted database: %s\n",
libsql_error_message(conn.err)
);
return 1;
}

const char *setup_sql = "CREATE TABLE IF NOT EXISTS secrets (id INTEGER "
"PRIMARY KEY AUTOINCREMENT, content TEXT);"
"INSERT INTO secrets (content) VALUES ('Top Secret "
"Info 1'), ('Classified Data 2');";

libsql_batch_t batch = libsql_connection_batch(conn, setup_sql);
if (batch.err) {
fprintf(
stderr,
"Error executing setup batch: %s\n",
libsql_error_message(batch.err)
);
return 1;
}

printf("Table created and data inserted into encrypted database.\n");

libsql_statement_t query_stmt =
libsql_connection_prepare(conn, "SELECT * FROM secrets");
if (query_stmt.err) {
fprintf(
stderr,
"Error preparing query: %s\n",
libsql_error_message(query_stmt.err)
);
return 1;
}

libsql_rows_t rows = libsql_statement_query(query_stmt);
if (rows.err) {
fprintf(
stderr,
"Error executing query: %s\n",
libsql_error_message(rows.err)
);
return 1;
}

printf("Secrets in the encrypted database:\n");
libsql_row_t row;
while (!(row = libsql_rows_next(rows)).err && !libsql_row_empty(row)) {
libsql_result_value_t id = libsql_row_value(row, 0);
libsql_result_value_t content = libsql_row_value(row, 1);

if (id.err || content.err) {
fprintf(stderr, "Error retrieving row values\n");
continue;
}

printf(
"%lld: %s\n",
(long long)id.ok.value.integer,
(char *)content.ok.value.text.ptr
);

libsql_row_deinit(row);
}

libsql_rows_deinit(rows);
libsql_statement_deinit(query_stmt);
libsql_connection_deinit(conn);
libsql_database_deinit(db);

printf("Database closed. The file 'encrypted.db' is now encrypted.\n");

return 0;
}
2 changes: 1 addition & 1 deletion examples/local/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Local

This example demonstrates how to use libsql with a local SQLite file.
This example demonstrates how to use libSQL with a local SQLite file.

## Building

Expand Down
1 change: 1 addition & 0 deletions examples/memory/Makefile
22 changes: 22 additions & 0 deletions examples/memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# In-Memory

This example demonstrates how to use libsql with an in-memory SQLite database.

## Building

```bash
make
```

## Running

```bash
./example
```

This example will:

1. Create an in-memory SQLite database.
2. Create a table called `users`.
3. Insert some sample data into the `users` table.
4. Query and display all users in the table.
91 changes: 91 additions & 0 deletions examples/memory/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "../../libsql.h"
#include <stdio.h>

int main() {
libsql_setup((libsql_config_t){0});

libsql_database_t db =
libsql_database_init((libsql_database_desc_t){.path = ":memory:"});
if (db.err) {
fprintf(
stderr,
"Error initializing in-memory database: %s\n",
libsql_error_message(db.err)
);
return 1;
}

libsql_connection_t conn = libsql_database_connect(db);
if (conn.err) {
fprintf(
stderr,
"Error connecting to in-memory database: %s\n",
libsql_error_message(conn.err)
);
return 1;
}

const char *setup_sql =
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);"
"INSERT INTO users (name) VALUES ('Alice'), ('Bob'), ('Charlie');";

libsql_batch_t batch = libsql_connection_batch(conn, setup_sql);
if (batch.err) {
fprintf(
stderr,
"Error executing setup batch: %s\n",
libsql_error_message(batch.err)
);
return 1;
}

printf("Table created and data inserted.\n");

libsql_statement_t query_stmt =
libsql_connection_prepare(conn, "SELECT * FROM users");
if (query_stmt.err) {
fprintf(
stderr,
"Error preparing query: %s\n",
libsql_error_message(query_stmt.err)
);
return 1;
}

libsql_rows_t rows = libsql_statement_query(query_stmt);
if (rows.err) {
fprintf(
stderr,
"Error executing query: %s\n",
libsql_error_message(rows.err)
);
return 1;
}

printf("Users in the in-memory database:\n");
libsql_row_t row;
while (!(row = libsql_rows_next(rows)).err && !libsql_row_empty(row)) {
libsql_result_value_t id = libsql_row_value(row, 0);
libsql_result_value_t name = libsql_row_value(row, 1);

if (id.err || name.err) {
fprintf(stderr, "Error retrieving row values\n");
continue;
}

printf(
"%lld: %s\n",
(long long)id.ok.value.integer,
(char *)name.ok.value.text.ptr
);

libsql_row_deinit(row);
}

libsql_rows_deinit(rows);
libsql_statement_deinit(query_stmt);
libsql_connection_deinit(conn);
libsql_database_deinit(db);

return 0;
}
2 changes: 1 addition & 1 deletion examples/remote/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Remote

This example demonstrates how to use libsql with a remote database.
This example demonstrates how to use libSQL with a remote database.

## Building

Expand Down
2 changes: 1 addition & 1 deletion examples/sync/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sync

This example demonstrates how to use libsql with a synced database (local file synced with a remote database).
This example demonstrates how to use libSQL with a synced database (local file synced with a remote database).

## Building

Expand Down
2 changes: 1 addition & 1 deletion examples/transactions/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Transactions

This example demonstrates how to use transactions with libsql. It shows how to start a transaction, perform multiple operations, and then commit or rollback the transaction.
This example demonstrates how to use transactions with libSQL.

## Building

Expand Down
2 changes: 1 addition & 1 deletion examples/vector/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Vector

This example demonstrates how to use libsql vector search with a local database.
This example demonstrates how to use libSQL vector search with a local database.

## Building

Expand Down

0 comments on commit be4e876

Please sign in to comment.