A database in TSV text files: data you can read, fast inserts, and an optional Rust speed-up.

0 downloads

dbbasic-tsv/examples/basic_usage.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-tsv.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-tsv.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-tsv.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add launch announcement for dbbasic-tsv v1.0.1 1c9b0b8 1 year, 1 month ago History
basic_usage.py · 2.6 KB · 95 lines
1#!/usr/bin/env python3
2"""
3Basic usage example for dbbasic-tsv
4Shows CRUD operations and basic queries
5"""
6
7import sys
8from pathlib import Path
9sys.path.insert(0, str(Path(__file__).parent.parent))
10
11from dbbasic.tsv import TSV
12
13
14def main():
15 print("dbbasic-tsv Basic Usage Example")
16 print("=" * 50)
17
18 # Create a table
19 print("\n1. Creating users table...")
20 users = TSV("users", ["id", "name", "email", "age"])
21
22 # Insert single record
23 print("\n2. Inserting single record...")
24 users.insert({
25 "id": "1",
26 "name": "Alice Smith",
27 "email": "alice@example.com",
28 "age": "30"
29 })
30 print(" ✓ Inserted Alice")
31
32 # Insert multiple records
33 print("\n3. Batch inserting records...")
34 records = [
35 {"id": "2", "name": "Bob Jones", "email": "bob@example.com", "age": "25"},
36 {"id": "3", "name": "Charlie Brown", "email": "charlie@example.com", "age": "35"},
37 {"id": "4", "name": "Diana Prince", "email": "diana@example.com", "age": "28"},
38 {"id": "5", "name": "Eve Anderson", "email": "eve@example.com", "age": "32"}
39 ]
40 count = users.insert_many(records)
41 print(f" ✓ Inserted {count} records")
42
43 # Query single record
44 print("\n4. Querying single record...")
45 user = users.query_one(id="1")
46 if user:
47 print(f" Found: {user['name']} ({user['email']})")
48
49 # Query multiple records
50 print("\n5. Querying multiple records...")
51 results = users.query(age="30")
52 print(f" Found {len(results)} users aged 30")
53 for user in results:
54 print(f" - {user['name']}")
55
56 # Update record
57 print("\n6. Updating record...")
58 updated = users.update({"id": "1"}, {"age": "31"})
59 print(f" ✓ Updated {updated} record(s)")
60
61 # Verify update
62 user = users.query_one(id="1")
63 print(f" Alice's new age: {user['age']}")
64
65 # Count records
66 print("\n7. Counting records...")
67 total = users.count()
68 print(f" Total users: {total}")
69
70 # Delete record
71 print("\n8. Deleting record...")
72 deleted = users.delete(id="5")
73 print(f" ✓ Deleted {deleted} record(s)")
74
75 # List all records
76 print("\n9. All remaining users:")
77 for user in users.all():
78 print(f" - {user['name']} ({user['age']} years)")
79
80 # Show table stats
81 print("\n10. Table statistics:")
82 stats = users.stats()
83 for key, value in stats.items():
84 print(f" {key}: {value}")
85
86 # Clean up (optional)
87 print("\n11. Cleaning up...")
88 users.drop()
89 print(" ✓ Table dropped")
90
91 print("\n✅ Example completed successfully!")
92
93
94if __name__ == "__main__":
95 main()