Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/batch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local.db-journal
19 changes: 19 additions & 0 deletions examples/batch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Local

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

## Install Dependencies

```bash
pip install libsql-experimental
```

## Running

Execute the example:

```bash
python3 main.py
```

This will create a local database, execute a batch of SQL statements (creating tables, inserting data, etc.), and then query the results.
16 changes: 16 additions & 0 deletions examples/batch/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import libsql_experimental as libsql

conn = libsql.connect("local.db")
cur = conn.cursor()

cur.executescript(
"""
DROP TABLE IF EXISTS users;
CREATE TABLE users (id INTEGER, name TEXT);
INSERT INTO users VALUES (1, '[email protected]');
INSERT INTO users VALUES (2, '[email protected]');
INSERT INTO users VALUES (3, '[email protected]');
"""
)

print(conn.execute("select * from users").fetchall())
1 change: 1 addition & 0 deletions examples/encryption/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local.db-journal
19 changes: 19 additions & 0 deletions examples/encryption/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Local

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

## Install Dependencies

```bash
pip install libsql-experimental
```

## Running

Execute the example:

```bash
python3 main.py
```

This will setup an encrypted SQLite database, execute a batch of SQL statements, and then query the results.
16 changes: 16 additions & 0 deletions examples/encryption/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import libsql_experimental as libsql

# You should set the ENCRYPTION_KEY in a environment variable
# For demo purposes, we're using a fixed key
encryption_key= "my-safe-encryption-key";

conn = libsql.connect("local.db", encryption_key=encryption_key)
cur = conn.cursor()

conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")


print(conn.execute("select * from users").fetchall())
1 change: 1 addition & 0 deletions examples/local/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local.db-journal
19 changes: 19 additions & 0 deletions examples/local/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Local

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

## Install Dependencies

```bash
pip install libsql-experimental
```

## Running

Execute the example:

```bash
python3 main.py
```

This will connect to a local SQLite, insert some data, and query it.
12 changes: 12 additions & 0 deletions examples/local/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import libsql_experimental as libsql

conn = libsql.connect("local.db")
cur = conn.cursor()

conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")


print(conn.execute("select * from users").fetchall())
19 changes: 19 additions & 0 deletions examples/memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Local

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

## Install Dependencies

```bash
pip install libsql-experimental
```

## Running

Execute the example:

```bash
python3 main.py
```

This will create an in-memory SQLite database, insert some data, and then query the results.
12 changes: 12 additions & 0 deletions examples/memory/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import libsql_experimental as libsql

conn = libsql.connect(":memory:")
cur = conn.cursor()

conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")


print(conn.execute("select * from users").fetchall())
19 changes: 19 additions & 0 deletions examples/remote/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Local

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

## Install Dependencies

```bash
pip install libsql-experimental
```

## Running

Execute the example:

```bash
TURSO_DATABASE_URL="..." TURSO_AUTH_TOKEN="..." python3 main.py
```

This will connect to a remote database, insert some data, and query it.
18 changes: 18 additions & 0 deletions examples/remote/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import libsql_experimental as libsql
import os

url = os.getenv("TURSO_DATABASE_URL")
auth_token = os.getenv("TURSO_AUTH_TOKEN")

conn = libsql.connect(url, auth_token=auth_token)
cur = conn.cursor()


conn.execute("DROP TABLE IF EXISTS users;")
conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")


print(conn.execute("select * from users").fetchall())
1 change: 1 addition & 0 deletions examples/sync/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local.db*
19 changes: 19 additions & 0 deletions examples/sync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Local

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

## Install Dependencies

```bash
pip install libsql-experimental
```

## Running

Execute the example:

```bash
TURSO_DATABASE_URL="..." TURSO_AUTH_TOKEN="..." python3 main.py
```

This will create a local database file that syncs with a remote database, insert some data, and query it.
19 changes: 19 additions & 0 deletions examples/sync/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import libsql_experimental as libsql
import os

url = os.getenv("TURSO_DATABASE_URL")
auth_token = os.getenv("TURSO_AUTH_TOKEN")

conn = libsql.connect("local.db", sync_url=url, auth_token=auth_token)
conn.sync()

cur = conn.cursor()

conn.execute("DROP TABLE IF EXISTS users;")
conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT);")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")


print(conn.execute("select * from users").fetchall())
1 change: 1 addition & 0 deletions examples/transaction/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local.db-journal
27 changes: 27 additions & 0 deletions examples/transaction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Local

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

## Install Dependencies

```bash
pip install libsql-experimental
```

## Running

Execute the example:

```bash
python3 main.py
```

This example will:

1. Create a new table called `users`.
2. Start a transaction.
3. Insert multiple users within the transaction.
4. Demonstrate how to rollback a transaction.
5. Start another transaction.
6. Insert more users and commit the transaction.
7. Query and display the final state of the `users` table.
15 changes: 15 additions & 0 deletions examples/transaction/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import libsql_experimental as libsql

conn = libsql.connect("local.db")
cur = conn.cursor()

conn.execute("DROP TABLE IF EXISTS users")
conn.execute("CREATE TABLE users (name TEXT);")
conn.execute("INSERT INTO users VALUES ('[email protected]');")
conn.execute("INSERT INTO users VALUES ('[email protected]');")

conn.rollback()

conn.execute("INSERT INTO users VALUES ('[email protected]');")

print(conn.execute("select * from users").fetchall())
1 change: 1 addition & 0 deletions examples/vector/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local.db-journal
19 changes: 19 additions & 0 deletions examples/vector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Local

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

## Install Dependencies

```bash
pip install libsql-experimental
```

## Running

Execute the example:

```bash
python3 main.py
```

This will setup a local SQLite database, insert some data, and then query the results using the vector similarity search function.
10 changes: 10 additions & 0 deletions examples/vector/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import libsql_experimental as libsql

conn = libsql.connect("local.db")

conn.execute("DROP TABLE IF EXISTS movies")
conn.execute("CREATE TABLE IF NOT EXISTS movies (title TEXT, year INT, embedding F32_BLOB(3))")
conn.execute("CREATE INDEX movies_idx ON movies (libsql_vector_idx(embedding))")
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]'))")

print(conn.execute("SELECT title, year FROM vector_top_k('movies_idx', '[4,5,6]', 3) JOIN movies ON movies.rowid = id").fetchall())
Loading