.query()Runs a set of SurrealQL statements against the database.
Method Syntaxdb.query(query, vars)
ImportantWhen multiple statements are provided,
.query()returns only the first statement’s result. If you need results from all statements, use.query_raw()instead.
| Arguments | Description | ||
|---|---|---|---|
query required | Specifies the SurrealQL statements. | ||
vars optional | Assigns variables which can be used in the query. |
result = await db.query( 'CREATE person SET name = $name', { "name": "John" } ) print(result)
Output:
[{'id': RecordID(table_name=person, record_id='opdhtxopqovbi3nvffid'), 'name': 'John'}]
.query_raw()With .query_raw(), you will get back the raw RPC response. In contrast to the .query() method, this will not throw for errors that occur in individual statements. Instead, errors are returned in the per-statement response, and the response includes the execution time for each statement.
raw = await db.query_raw( 'CREATE person SET name = $name; SELECT * FROM person;', { "name": "John" } ) # `raw` contains results for *all* statements in the query. print(raw)
Output (formatted for readability):
{ 'id': 'e66e2607-0497-4b50-868c-5c61ddf495ca', 'result': [ { 'result': [{'id': RecordID(table_name=person, record_id='aykahjapj2g2ra23xflx'), 'name': 'John'}], 'status': 'OK', 'time': '3.357375ms' }, { 'result': [{'id': RecordID(table_name=person, record_id='aykahjapj2g2ra23xflx'), 'name': 'John'}], 'status': 'OK', 'time': '881.75µs' } ] }