-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_unified.ruff
More file actions
69 lines (58 loc) · 2.43 KB
/
Copy pathdatabase_unified.ruff
File metadata and controls
69 lines (58 loc) · 2.43 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
59
60
61
62
63
64
65
66
67
68
69
# Unified Database API Example
# Demonstrates the new db_connect(db_type, connection_string) syntax
# that supports multiple database backends (SQLite, PostgreSQL, MySQL)
print("=== Unified Database API Demo ===")
print("")
# Connect to SQLite database using the unified API
print("Connecting to SQLite database...")
db := db_connect("sqlite", "test_unified.db")
print("Connected!")
print("")
# Create a users table
print("Creating users table...")
db_execute(db, "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)", [])
print("")
# Insert some test data
print("Inserting test users...")
db_execute(db, "INSERT INTO users (name, email) VALUES (?, ?)", ["Alice Johnson", "alice@example.com"])
db_execute(db, "INSERT INTO users (name, email) VALUES (?, ?)", ["Bob Smith", "bob@example.com"])
db_execute(db, "INSERT INTO users (name, email) VALUES (?, ?)", ["Carol Martinez", "carol@example.com"])
print("Inserted 3 users")
print("")
# Query all users
print("Querying all users:")
users := db_query(db, "SELECT * FROM users", [])
for user in users {
print(" ID: " + to_string(user["id"]) + ", Name: " + user["name"] + ", Email: " + user["email"])
}
print("")
# Query specific user with parameters
print("Querying user with name 'Bob Smith':")
bob := db_query(db, "SELECT * FROM users WHERE name = ?", ["Bob Smith"])
if len(bob) > 0 {
print(" Found: " + bob[0]["name"] + " (" + bob[0]["email"] + ")")
}
print("")
# Update a user
print("Updating Alice's email...")
rows_affected := db_execute(db, "UPDATE users SET email = ? WHERE name = ?", ["alice.j@newmail.com", "Alice Johnson"])
print(" Updated " + to_string(rows_affected) + " row(s)")
print("")
# Verify the update
alice := db_query(db, "SELECT * FROM users WHERE name = ?", ["Alice Johnson"])
print("Alice's new email: " + alice[0]["email"])
print("")
# Count total users
count_result := db_query(db, "SELECT COUNT(*) as total FROM users", [])
print("Total users in database: " + to_string(count_result[0]["total"]))
print("")
# Close the database connection
db_close(db)
print("Database connection closed")
print("")
# NOTE: PostgreSQL and MySQL support coming soon!
# Future syntax will be:
# db := db_connect("postgres", "host=localhost dbname=myapp user=admin password=secret")
# db := db_connect("mysql", "mysql://user:pass@localhost:3306/myapp")
print("✓ SQLite database operations completed successfully!")
print("✓ PostgreSQL and MySQL support coming in future release")