Skip to content

Commit 16a6b1b

Browse files
authored
Merge pull request #73 from giovannibenussi/examples
Add Usage Examples
2 parents ff13876 + d031c9b commit 16a6b1b

File tree

22 files changed

+284
-0
lines changed

22 files changed

+284
-0
lines changed

examples/batch/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db-journal

examples/batch/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Local
2+
3+
This example demonstrates how to use libSQL to execute a batch of SQL statements.
4+
5+
## Install Dependencies
6+
7+
```bash
8+
pip install libsql-experimental
9+
```
10+
11+
## Running
12+
13+
Execute the example:
14+
15+
```bash
16+
python3 main.py
17+
```
18+
19+
This will create a local database, execute a batch of SQL statements (creating tables, inserting data, etc.), and then query the results.

examples/batch/main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import libsql_experimental as libsql
2+
3+
conn = libsql.connect("local.db")
4+
cur = conn.cursor()
5+
6+
cur.executescript(
7+
"""
8+
DROP TABLE IF EXISTS users;
9+
CREATE TABLE users (id INTEGER, name TEXT);
10+
INSERT INTO users VALUES (1, '[email protected]');
11+
INSERT INTO users VALUES (2, '[email protected]');
12+
INSERT INTO users VALUES (3, '[email protected]');
13+
"""
14+
)
15+
16+
print(conn.execute("select * from users").fetchall())

examples/encryption/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db-journal

examples/encryption/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Local
2+
3+
This example demonstrates how to create and use an encrypted SQLite database with libSQL.
4+
5+
## Install Dependencies
6+
7+
```bash
8+
pip install libsql-experimental
9+
```
10+
11+
## Running
12+
13+
Execute the example:
14+
15+
```bash
16+
python3 main.py
17+
```
18+
19+
This will setup an encrypted SQLite database, execute a batch of SQL statements, and then query the results.

examples/encryption/main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import libsql_experimental as libsql
2+
3+
# You should set the ENCRYPTION_KEY in a environment variable
4+
# For demo purposes, we're using a fixed key
5+
encryption_key= "my-safe-encryption-key";
6+
7+
conn = libsql.connect("local.db", encryption_key=encryption_key)
8+
cur = conn.cursor()
9+
10+
conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
11+
conn.execute("INSERT INTO users VALUES ('[email protected]');")
12+
conn.execute("INSERT INTO users VALUES ('[email protected]');")
13+
conn.execute("INSERT INTO users VALUES ('[email protected]');")
14+
15+
16+
print(conn.execute("select * from users").fetchall())

examples/local/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db-journal

examples/local/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Local
2+
3+
This example demonstrates how to use libSQL with a local SQLite file.
4+
5+
## Install Dependencies
6+
7+
```bash
8+
pip install libsql-experimental
9+
```
10+
11+
## Running
12+
13+
Execute the example:
14+
15+
```bash
16+
python3 main.py
17+
```
18+
19+
This will connect to a local SQLite, insert some data, and query it.

examples/local/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import libsql_experimental as libsql
2+
3+
conn = libsql.connect("local.db")
4+
cur = conn.cursor()
5+
6+
conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
7+
conn.execute("INSERT INTO users VALUES ('[email protected]');")
8+
conn.execute("INSERT INTO users VALUES ('[email protected]');")
9+
conn.execute("INSERT INTO users VALUES ('[email protected]');")
10+
11+
12+
print(conn.execute("select * from users").fetchall())

examples/memory/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Local
2+
3+
This example demonstrates how to use libSQL with an in-memory SQLite database.
4+
5+
## Install Dependencies
6+
7+
```bash
8+
pip install libsql-experimental
9+
```
10+
11+
## Running
12+
13+
Execute the example:
14+
15+
```bash
16+
python3 main.py
17+
```
18+
19+
This will create an in-memory SQLite database, insert some data, and then query the results.

0 commit comments

Comments
 (0)