SimpleClient Blog¶
Same blog engine as the low-level blog.py example, but rewritten
using the SimpleClient wrapper with auto-created tables.
Full Source¶
1"""
2SimpleClient Blog Example
3
4A blog engine using the SimpleClient wrapper with auto-created tables.
5"""
6
7import sqbooster
8
9
10def main():
11 with sqbooster.sqlite() as db:
12
13 print("=== SimpleClient Blog ===\n")
14
15 db.add("posts", [
16 {"title": "Getting Started with sqbooster",
17 "slug": "getting-started",
18 "body": "sqbooster makes database access easy...",
19 "author": "Alice", "status": "published", "views": 150},
20 {"title": "Advanced Querying",
21 "slug": "advanced-querying",
22 "body": "Filters, ordering, pagination...",
23 "author": "Bob", "status": "published", "views": 89},
24 {"title": "Draft: Future Plans",
25 "slug": "future-plans",
26 "body": "Coming soon...",
27 "author": "Alice", "status": "draft", "views": 0},
28 ])
29
30 db.add("comments", [
31 {"post_id": 1, "author": "Charlie", "body": "Great tutorial!"},
32 {"post_id": 1, "author": "Diana", "body": "Very helpful!"},
33 {"post_id": 2, "author": "Eve", "body": "Clean API."},
34 ])
35
36 print("--- Published Posts ---")
37 published = db.find("posts", status="published", order_by="-views")
38 for post in published:
39 print(f" [{post['views']} views] {post['title']} by {post['author']}")
40
41 print("\n--- Search: 'query' in title ---")
42 for post in db.find("posts", title__contains="query"):
43 print(f" {post['title']}")
44
45 print("\n--- Page 1 (limit 2) ---")
46 for post in db.find("posts", order_by="title", limit=2):
47 print(f" {post['title']}")
48
49 print(f"\nPublished: {db.count('posts', status='published')}")
50 print(f"Drafts: {db.count('posts', status='draft')}")
51
52 print("\n--- Comments for Post #1 ---")
53 for c in db.find("comments", post_id=1):
54 print(f" {c['author']}: {c['body']}")
55
56 db.update("posts", {"views": 151}, slug="getting-started")
57 post = db.get("posts", slug="getting-started")
58 print(f"\nUpdated views: {post['views']}")
59
60 deleted = db.remove("posts", status="draft")
61 print(f"Deleted {deleted} draft(s)")
62 print(f"Remaining posts: {db.count('posts')}")
63
64 print("\nDone!")
65
66
67if __name__ == "__main__":
68 main()
What It Demonstrates¶
Auto-created tables - no
define()needed, tables created from the dataTop-level factory -
sqbooster.sqlite()with no backend importsBulk insert with a single
add()callFind with filters, ordering, and pagination in one line
Count, get, update, remove - all as simple method calls
Context manager for automatic cleanup
Running the Example¶
python examples/simple_blog.py