-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathhello.py
More file actions
58 lines (44 loc) · 1.91 KB
/
hello.py
File metadata and controls
58 lines (44 loc) · 1.91 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
49
50
51
52
53
54
55
56
57
58
"""Minimal walkthrough of the SQLRite Python bindings.
Run after:
cd sdk/python
maturin develop
python examples/python/hello.py
The shape mirrors the stdlib `sqlite3` module — if you've used that,
you already know how to drive this.
"""
import sqlrite
def main() -> None:
# Use `:memory:` for a transient in-memory DB (matching sqlite3
# convention); pass a path like "foo.sqlrite" for a file-backed
# one that auto-saves on every write.
with sqlrite.connect(":memory:") as conn:
cur = conn.cursor()
cur.execute(
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)"
)
cur.execute("INSERT INTO users (name, age) VALUES ('alice', 30)")
cur.execute("INSERT INTO users (name, age) VALUES ('bob', 25)")
cur.execute("INSERT INTO users (name, age) VALUES ('carol', 40)")
# `.description` exposes PEP 249 column metadata — a list of
# 7-tuples, name in position 0, rest None until we track
# types.
cur.execute("SELECT id, name, age FROM users")
print("Columns:", [col[0] for col in cur.description])
# Iterate tuples DB-API-style.
print("\nAll users:")
for row in cur:
uid, name, age = row
print(f" {uid}: {name} ({age})")
# Transactions: BEGIN + INSERT + ROLLBACK leaves the table
# unchanged. (`commit()` / `rollback()` work on the
# Connection; the `with` block auto-commits on clean exit
# and rolls back on exception.)
cur.execute("BEGIN")
cur.execute("INSERT INTO users (name, age) VALUES ('phantom', 99)")
cur.execute("SELECT id FROM users")
print(f"\nMid-transaction row count: {len(cur.fetchall())}")
conn.rollback()
cur.execute("SELECT id FROM users")
print(f"Post-rollback row count: {len(cur.fetchall())}")
if __name__ == "__main__":
main()