8000 Merge pull request #73 from giovannibenussi/examples · tursodatabase/libsql-python@16a6b1b · GitHub
[go: up one dir, main page]

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, 'first@example.org');
11+
INSERT INTO users VALUES (2, 'second@example.org');
12+
INSERT INTO users VALUES (3, 'third@example.org');
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 ('first@example.com');")
12+
conn.execute("INSERT INTO users VALUES ('second@example.com');")
13+
conn.execute("INSERT INTO users VALUES ('third@example.com');")
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 ('first@example.com');")
8+
conn.execute("INSERT INTO users VALUES ('second@example.com');")
9+
conn.execute("INSERT INTO users VALUES ('third@example.com');")
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.

examples/memory/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(":memory:")
4+
cur = conn.cursor()
5+
6+
conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
7+
conn.execute("INSERT INTO users VALUES ('first@example.com');")
8+
conn.execute("INSERT INTO users VALUES ('second@example.com');")
9+
conn.execute("INSERT INTO users VALUES ('third@example.com');")
10+
11+
12+
print(conn.execute("select * from users").fetchall())

examples/remote/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 remote 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+
TURSO_DATABASE_URL="..." TURSO_AUTH_TOKEN="..." python3 main.py
17+
```
18+
19+
This will connect to a remote database, insert some data, and query it.

examples/remote/main.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import libsql_experimental as libsql
2+
import os
3+
4+
url = os.getenv("TURSO_DATABASE_URL")
5+
auth_token = os.getenv("TURSO_AUTH_TOKEN")
6+
7+
conn = libsql.connect(url, auth_token=auth_token)
8+
cur = conn.cursor()
9+
10+
11+
conn.execute("DROP TABLE IF EXISTS users;")
12+
conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
13+
conn.execute("INSERT INTO users VALUES ('first@example.com');")
14+
conn.execute("INSERT INTO users VALUES ('second@example.com');")
15+
conn.execute("INSERT INTO users VALUES ('third@example.com');")
16+
17+
18+
print(conn.execute("select * from users").fetchall())

examples/sync/.gitignore

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

examples/sync/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 synced database (local file synced with a remote 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+
TURSO_DATABASE_URL="..." TURSO_AUTH_TOKEN="..." python3 main.py
17+
```
18+
19+
This will create a local database file that syncs with a remote database, insert some data, and query it.

examples/sync/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import libsql_experimental as libsql
2+
import os
3+
4+
url = os.getenv("TURSO_DATABASE_URL")
5+
auth_token = os.getenv("TURSO_AUTH_TOKEN")
6+
7+
conn = libsql.connect("local.db", sync_url=url, auth_token=auth_token)
8+
conn.sync()
9+
10+
cur = conn.cursor()
11+
12+
conn.execute("DROP TABLE IF EXISTS users;")
13+
conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
14+
conn.execute("INSERT INTO users VALUES ('first@example.com');")
15+
conn.execute("INSERT INTO users VALUES ('second@example.com');")
16+
conn.execute("INSERT INTO users VALUES ('third@example.com');")
17+
18+
19+
print(conn.execute("select * from users").fetchall())

examples/transaction/.gitignore

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

examples/transaction/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 example will:
20+
21+
1. Create a new table called `users`.
22+
2. Start a transaction.
23+
3. Insert multiple users within the transaction.
24+
4. Demonstrate how to rollback a transaction.
25+
5. Start another transaction.
26+
6. Insert more users and commit the transaction.
27+
7. Query and display the final state of the `users` table.

examples/transaction/main.py

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

examples/vector/.gitignore

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

examples/vector/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 vector search with a local 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 setup a local SQLite database, insert some data, and then query the results using the vector similarity search function.

examples/vector/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import libsql_experimental as libsql
2+
3+
conn = libsql.connect("local.db")
4+
5+
conn.execute("DROP TABLE IF EXISTS movies")
6+
conn.execute("CREATE TABLE IF NOT EXISTS movies (title TEXT, year INT, embedding F32_BLOB(3))")
7+
conn.execute("CREATE INDEX movies_idx ON movies (libsql_vector_idx(embedding))")
8+
conn.execute("INSERT INTO movies (title, year, embedding) VALUES ('Napoleon', 2023, vector32('[1,2,3]')), ('Black Hawk Down', 2001, vector32('[10,11,12]')), ('Gladiator', 2000, vector32('[7,8,9]')), ('Blade Runner', 1982, vector32('[4,5,6]'))")
9+
10+
print(conn.execute("SELECT title, year FROM vector_top_k('movies_idx', '[4,5,6]', 3) JOIN movies ON movies.rowid = id").fetchall())

0 commit comments

Comments
 (0)
0