Skip to content

Commit be4e876

Browse files
committed
examples: update readmes
1 parent 44c4fb6 commit be4e876

File tree

13 files changed

+252
-12
lines changed

13 files changed

+252
-12
lines changed

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@
136136
137137
## Examples
138138
139-
- [Local](/examples/local)
140-
- [Remote](/examples/remote)
141-
- [Sync](/examples/sync)
142-
- [Vector](/examples/vector)
143-
- [Transactions](/examples/transactions)
144-
- [Batch](/examples/batch)
139+
| Example | Description |
140+
| ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
141+
| [local](examples/local) | Demonstrates how to use libsql with a local SQLite database file. Creates a database, inserts data, and performs queries. |
142+
| [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. |
143+
| [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. |
144+
| [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. |
145+
| [transactions](examples/transactions) | Shows how to use transactions in libsql. Demonstrates starting a transaction, performing multiple operations, and committing or rolling back changes. |
146+
| [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. |
147+
| [vector](examples/vector) | Demonstrates how to work with vector embeddings in libsql, including storing and querying vector data for similarity search. |
148+
| [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. |
145149
146150
## Documentation
147151

examples/batch/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Batch
22

3-
This example demonstrates how to use libsql to execute a batch of SQL statements.
3+
This example demonstrates how to use libSQL to execute a batch of SQL statements.
44

55
## Building
66

examples/encryption/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Makefile.common

examples/encryption/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Encryption
2+
3+
This example demonstrates how to create and use an encrypted SQLite database with libSQL.
4+
5+
## Building
6+
7+
```bash
8+
make
9+
```
10+
11+
## Running
12+
13+
```bash
14+
./example
15+
```
16+
17+
This example will:
18+
19+
1. Create an encrypted SQLite database file named `encrypted.db`.
20+
2. Create a table called `secrets`.
21+
3. Insert some sample data into the `secrets` table.
22+
4. Query and display all secrets in the table.

examples/encryption/example.c

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "../../libsql.h"
2+
#include <stdio.h>
3+
4+
int main() {
5+
libsql_setup((libsql_config_t){0});
6+
7+
const char *encryption_key = "my_secret_key";
8+
9+
libsql_database_t db = libsql_database_init((libsql_database_desc_t
10+
){.path = "encrypted.db",
11+
.encryption_key = encryption_key,
12+
.cypher = LIBSQL_CYPHER_AES256});
13+
14+
if (db.err) {
15+
fprintf(
16+
stderr,
17+
"Error initializing encrypted database: %s\n",
18+
libsql_error_message(db.err)
19+
);
20+
return 1;
21+
}
22+
23+
libsql_connection_t conn = libsql_database_connect(db);
24+
if (conn.err) {
25+
fprintf(
26+
stderr,
27+
"Error connecting to encrypted database: %s\n",
28+
libsql_error_message(conn.err)
29+
);
30+
return 1;
31+
}
32+
33+
const char *setup_sql = "CREATE TABLE IF NOT EXISTS secrets (id INTEGER "
34+
"PRIMARY KEY AUTOINCREMENT, content TEXT);"
35+
"INSERT INTO secrets (content) VALUES ('Top Secret "
36+
"Info 1'), ('Classified Data 2');";
37+
38+
libsql_batch_t batch = libsql_connection_batch(conn, setup_sql);
39+
if (batch.err) {
40+
fprintf(
41+
stderr,
42+
"Error executing setup batch: %s\n",
43+
libsql_error_message(batch.err)
44+
);
45+
return 1;
46+
}
47+
48+
printf("Table created and data inserted into encrypted database.\n");
49+
50+
libsql_statement_t query_stmt =
51+
libsql_connection_prepare(conn, "SELECT * FROM secrets");
52+
if (query_stmt.err) {
53+
fprintf(
54+
stderr,
55+
"Error preparing query: %s\n",
56+
libsql_error_message(query_stmt.err)
57+
);
58+
return 1;
59+
}
60+
61+
libsql_rows_t rows = libsql_statement_query(query_stmt);
62+
if (rows.err) {
63+
fprintf(
64+
stderr,
65+
"Error executing query: %s\n",
66+
libsql_error_message(rows.err)
67+
);
68+
return 1;
69+
}
70+
71+
printf("Secrets in the encrypted database:\n");
72+
libsql_row_t row;
73+
while (!(row = libsql_rows_next(rows)).err && !libsql_row_empty(row)) {
74+
libsql_result_value_t id = libsql_row_value(row, 0);
75+
libsql_result_value_t content = libsql_row_value(row, 1);
76+
77+
if (id.err || content.err) {
78+
fprintf(stderr, "Error retrieving row values\n");
79+
continue;
80+
}
81+
82+
printf(
83+
"%lld: %s\n",
84+
(long long)id.ok.value.integer,
85+
(char *)content.ok.value.text.ptr
86+
);
87+
88+
libsql_row_deinit(row);
89+
}
90+
91+
libsql_rows_deinit(rows);
92+
libsql_statement_deinit(query_stmt);
93+
libsql_connection_deinit(conn);
94+
libsql_database_deinit(db);
95+
96+
printf("Database closed. The file 'encrypted.db' is now encrypted.\n");
97+
98+
return 0;
99+
}

examples/local/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Local
22

3-
This example demonstrates how to use libsql with a local SQLite file.
3+
This example demonstrates how to use libSQL with a local SQLite file.
44

55
## Building
66

examples/memory/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Makefile.common

examples/memory/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# In-Memory
2+
3+
This example demonstrates how to use libsql with an in-memory SQLite database.
4+
5+
## Building
6+
7+
```bash
8+
make
9+
```
10+
11+
## Running
12+
13+
```bash
14+
./example
15+
```
16+
17+
This example will:
18+
19+
1. Create an in-memory SQLite database.
20+
2. Create a table called `users`.
21+
3. Insert some sample data into the `users` table.
22+
4. Query and display all users in the table.

examples/memory/example.c

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "../../libsql.h"
2+
#include <stdio.h>
3+
4+
int main() {
5+
libsql_setup((libsql_config_t){0});
6+
7+
libsql_database_t db =
8+
libsql_database_init((libsql_database_desc_t){.path = ":memory:"});
9+
if (db.err) {
10+
fprintf(
11+
stderr,
12+
"Error initializing in-memory database: %s\n",
13+
libsql_error_message(db.err)
14+
);
15+
return 1;
16+
}
17+
18+
libsql_connection_t conn = libsql_database_connect(db);
19+
if (conn.err) {
20+
fprintf(
21+
stderr,
22+
"Error connecting to in-memory database: %s\n",
23+
libsql_error_message(conn.err)
24+
);
25+
return 1;
26+
}
27+
28+
const char *setup_sql =
29+
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);"
30+
"INSERT INTO users (name) VALUES ('Alice'), ('Bob'), ('Charlie');";
31+
32+
libsql_batch_t batch = libsql_connection_batch(conn, setup_sql);
33+
if (batch.err) {
34+
fprintf(
35+
stderr,
36+
"Error executing setup batch: %s\n",
37+
libsql_error_message(batch.err)
38+
);
39+
return 1;
40+
}
41+
42+
printf("Table created and data inserted.\n");
43+
44+
libsql_statement_t query_stmt =
45+
libsql_connection_prepare(conn, "SELECT * FROM users");
46+
if (query_stmt.err) {
47+
fprintf(
48+
stderr,
49+
"Error preparing query: %s\n",
50+
libsql_error_message(query_stmt.err)
51+
);
52+
return 1;
53+
}
54+
55+
libsql_rows_t rows = libsql_statement_query(query_stmt);
56+
if (rows.err) {
57+
fprintf(
58+
stderr,
59+
"Error executing query: %s\n",
60+
libsql_error_message(rows.err)
61+
);
62+
return 1;
63+
}
64+
65+
printf("Users in the in-memory database:\n");
66+
libsql_row_t row;
67+
while (!(row = libsql_rows_next(rows)).err && !libsql_row_empty(row)) {
68+
libsql_result_value_t id = libsql_row_value(row, 0);
69+
libsql_result_value_t name = libsql_row_value(row, 1);
70+
71+
if (id.err || name.err) {
72+
fprintf(stderr, "Error retrieving row values\n");
73+
continue;
74+
}
75+
76+
printf(
77+
"%lld: %s\n",
78+
(long long)id.ok.value.integer,
79+
(char *)name.ok.value.text.ptr
80+
);
81+
82+
libsql_row_deinit(row);
83+
}
84+
85+
libsql_rows_deinit(rows);
86+
libsql_statement_deinit(query_stmt);
87+
libsql_connection_deinit(conn);
88+
libsql_database_deinit(db);
89+
90+
return 0;
91+
}

examples/remote/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Remote
22

3-
This example demonstrates how to use libsql with a remote database.
3+
This example demonstrates how to use libSQL with a remote database.
44

55
## Building
66

examples/sync/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Sync
22

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

55
## Building
66

examples/transactions/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Transactions
22

3-
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.
3+
This example demonstrates how to use transactions with libSQL.
44

55
## Building
66

examples/vector/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Vector
22

3-
This example demonstrates how to use libsql vector search with a local database.
3+
This example demonstrates how to use libSQL vector search with a local database.
44

55
## Building
66

0 commit comments

Comments
 (0)