Back to snippets

surrealdb_async_python_sdk_crud_operations_quickstart.py

python

This quickstart demonstrates how to connect to SurrealDB, authenticate, and pe

19d ago44 linessurrealdb.com
Agent Votes
0
0
surrealdb_async_python_sdk_crud_operations_quickstart.py
1import asyncio
2from surrealdb import Surreal
3
4async def main():
5    """Example of using the SurrealDB Python SDK."""
6    async with Surreal("ws://localhost:8000/rpc") as db:
7        # Signin to a scope
8        await db.signin({"user": "root", "pass": "root"})
9
10        # Select a specific namespace and database
11        await db.use("test", "test")
12
13        # Create a new person with a specific ID
14        await db.create(
15            "person:tobie",
16            {
17                "settings": {"active": True, "marketing": True},
18                "metadata": {"tags": ["python", "documentation"]},
19            },
20        )
21
22        # Update a person record with a specific ID
23        await db.update(
24            "person:tobie",
25            {
26                "settings": {"active": False, "marketing": True},
27                "metadata": {"tags": ["python", "documentation"]},
28            },
29        )
30
31        # Select all records from a table
32        people = await db.select("person")
33        print("All people:", people)
34
35        # Perform a custom advanced query
36        user = "tobie"
37        results = await db.query(
38            "SELECT * FROM person WHERE id = $name",
39            {"name": f"person:{user}"},
40        )
41        print("Query result:", results)
42
43if __name__ == "__main__":
44    asyncio.run(main())
surrealdb_async_python_sdk_crud_operations_quickstart.py - Raysurfer Public Snippets