SimpleClient Key-Value + Tables

Combines the simple key-value API with auto-created tables in one client. Ideal for applications that need both structured data and fast key lookups.

Full Source

 1"""
 2SimpleClient Key-Value Example
 3
 4Combines the simple key-value API with auto-created tables.
 5"""
 6
 7import sqbooster
 8
 9
10def main():
11    with sqbooster.sqlite() as db:
12
13        print("=== Key-Value + Tables ===\n")
14
15        db.write("app:name", "MyApp")
16        db.write("app:version", "2.0.0")
17        db.write("app:config", {"theme": "dark", "lang": "en"})
18
19        print("--- Key-Value Store ---")
20        print(f"  App name: {db.read('app:name')}")
21        print(f"  Version: {db.read('app:version')}")
22        print(f"  Config: {db.read('app:config')}")
23        print(f"  All keys: {db.keys('app:')}")
24
25        db.add("users", [
26            {"name": "Alice", "email": "alice@app.com", "role": "admin"},
27            {"name": "Bob", "email": "bob@app.com", "role": "user"},
28        ])
29
30        db.add("logs", [
31            {"level": "INFO", "message": "App started"},
32            {"level": "ERROR", "message": "Connection failed"},
33            {"level": "INFO", "message": "User logged in"},
34        ])
35
36        print("\n--- Structured Tables ---")
37        admins = db.find("users", role="admin")
38        print(f"  Admins: {[u['name'] for u in admins]}")
39
40        errors = db.find("logs", level="ERROR")
41        print(f"  Errors: {[l['message'] for l in errors]}")
42
43        print(f"  Total logs: {db.count('logs')}")
44
45        print("\n--- Combined Workflow ---")
46        config = db.read("app:config")
47        if config.get("theme") == "dark":
48            db.add("logs", {"level": "INFO", "message": "Dark theme active"})
49
50        db.update("users", {"role": "superadmin"}, name="Alice")
51        alice = db.get("users", name="Alice")
52        print(f"  Alice is now: {alice['role']}")
53
54        db.delete_key("app:version")
55        print(f"  Version after delete: {db.read('app:version')}")
56
57        print(f"\n  Tables: {db.tables()}")
58        print(f"  KV keys: {db.keys()}")
59
60    print("\nDone!")
61
62
63if __name__ == "__main__":
64    main()

What It Demonstrates

  • Key-value store for config, cache, and simple lookups

  • Auto-created tables for structured data with queries

  • Using both in a single workflow - read config from KV, write logs to a table

  • keys() with pattern matching for namespaced lookups

  • delete_key() for KV cleanup

Running the Example

python examples/simple_keyvalue.py