SimpleClient Backend Switch

The same application logic runs against SQLite, JSON, and Pickle backends. Switching databases requires changing only the constructor call.

Full Source

 1"""
 2SimpleClient Backend Switch Example
 3
 4The same code runs against SQLite, JSON, and Pickle backends.
 5Switching databases requires changing only one line.
 6"""
 7
 8import tempfile
 9import os
10import sqbooster
11
12
13def run_app(db, label):
14    print(f"\n--- {label} ---")
15
16    db.add("users", [
17        {"name": "Alice", "age": 30, "active": True},
18        {"name": "Bob", "age": 25, "active": True},
19        {"name": "Charlie", "age": 35, "active": False},
20    ])
21
22    adults = db.find("users", age__gte=18, order_by="name")
23    print(f"  Adults: {[u['name'] for u in adults]}")
24
25    active = db.find("users", active=True)
26    print(f"  Active: {len(active)}")
27
28    db.update("users", {"age": 31}, name="Alice")
29    alice = db.get("users", name="Alice")
30    print(f"  Alice's new age: {alice['age']}")
31
32    db.remove("users", name="Charlie")
33    print(f"  Remaining: {db.count('users')}")
34
35
36def main():
37    print("=== Backend Switch Example ===")
38
39    with sqbooster.sqlite() as db:
40        run_app(db, "SQLite")
41
42    tmp_json = tempfile.NamedTemporaryFile(suffix=".json", delete=False)
43    tmp_json.close()
44    with sqbooster.json(tmp_json.name) as db:
45        run_app(db, "JSON File")
46    os.unlink(tmp_json.name)
47
48    tmp_pkl = tempfile.NamedTemporaryFile(suffix=".pkl", delete=False)
49    tmp_pkl.close()
50    with sqbooster.pickle(tmp_pkl.name) as db:
51        run_app(db, "Pickle File")
52    os.unlink(tmp_pkl.name)
53
54    print("\nDone! All backends produce identical results.")
55
56
57if __name__ == "__main__":
58    main()

What It Demonstrates

  • One-line backend switch - same SimpleClient API everywhere

  • Top-level functions: sqbooster.sqlite(), sqbooster.json(), etc.

  • Auto-created tables work identically across all backends

  • Context manager for automatic resource cleanup

Running the Example

python examples/simple_backend_switch.py