Skip to content

Commit

Permalink
Add examplesg
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannibenussi committed Oct 16, 2024
1 parent ff13876 commit c26dd86
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 0 deletions.
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())

0 comments on commit c26dd86

Please sign in to comment.