-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (33 loc) · 1.25 KB
/
main.py
File metadata and controls
48 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Local Example: Using libSQL with a synced Turso database
This script demonstrates how to use libSQL with a local database file
that syncs with a remote Turso database.
Requirements:
- An existing database on Turso cloud.
- A `.env` file with your Turso database credentials.
Setup:
1. Create a new database with:
turso db create <your_database_name>
2. Get your database URL:
turso db show <your_database_name>
3. Create an auth token:
turso db tokens create <your_database_name>
4. Add the URL and token to a `.env` file in your project root:
TURSO_DATABASE_URL=<your_database_url>
TURSO_AUTH_TOKEN=<your_auth_token>
"""
import libsql
import os
from dotenv import load_dotenv
load_dotenv()
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 ('first@example.com');")
conn.execute("INSERT INTO users VALUES ('second@example.com');")
conn.execute("INSERT INTO users VALUES ('third@example.com');")
print(conn.execute("select * from users").fetchall())