query_multiple
query_multiple
can execute multiple queries with the same cursor and serialize the results. This method
will throw a ValueError
if you don't supply the same number of queries and models.
Parameters🔗
name | type | description | optional | default |
---|---|---|---|---|
sql | str |
the sql query str to execute | ||
param | ParamType |
params to substitute in the query | None |
|
model | Any |
the callable to serialize the model; callable must be able to accept column names as kwargs. | dict |
Example🔗
Query two tables and return the serialized results.
import datetime
from dataclasses import dataclass
from pydapper import connect
@dataclass
class Owner:
id: int
name: str
@dataclass
class Task:
id: int
description: str
due_date: datetime.date
owner_id: int
with connect() as commands:
task, owner = commands.query_multiple(
("select * from task limit 1", "select * from owner limit 1"), models=(Task, Owner)
)
print(task)
# [Task(id=1, description='Set up a test database', due_date=datetime.date(2021, 12, 31), owner_id=1)]
print(owner)
# [Owner(id=1, name='Zach Schumacher')]