Skip to content

execute_async

execute_async can execute a command one or multiple times and return the number of affected rows. This method is usually used to execute insert, update or delete operations.

Parameters🔗

name type description optional default
sql str the sql query str to execute 👎
param ListParamType, ParamType params to substitute in the query 👍 None

Example - Execute Insert🔗

Single🔗

Execute the INSERT statement a single time.

import asyncio
import datetime

from pydapper import connect_async


async def main():
    async with connect_async() as commands:
        rowcount = await commands.execute_async(
            "insert into task (description, due_date, owner_id) values (?description?, ?due_date?, ?owner_id?)",
            param={"description": "An insert example", "due_date": datetime.date.today(), "owner_id": 1},
        )

    print(rowcount)
    # 1


asyncio.run(main())
(This script is complete, it should run "as is")

Multiple🔗

Execute the INSERT statement multiple times, one for each object in the param list.

import asyncio
import datetime

from pydapper import connect_async


async def main():
    async with connect_async() as commands:
        rowcount = await commands.execute_async(
            "insert into task (description, due_date, owner_id) values (?description?, ?due_date?, ?owner_id?)",
            param=[
                {"description": "An insert example", "due_date": datetime.date.today(), "owner_id": 1},
                {"description": "With multiple inserts!", "due_date": datetime.date.today(), "owner_id": 1},
            ],
        )

    print(rowcount)
    # 2


asyncio.run(main())
(This script is complete, it should run "as is")

Example - Execute Update🔗

Single🔗

Execute the UPDATE statement a single time.

import asyncio

from pydapper import connect_async


async def main():
    async with connect_async() as commands:
        rowcount = await commands.execute_async(
            "update task set description = ?desc? where id = ?id?", param={"desc": "A single update!", "id": 1}
        )

    print(rowcount)
    # 1


asyncio.run(main())
(This script is complete, it should run "as is")

Multiple🔗

Execute the UPDATE statement multiple times, one for each object in the param list.

import asyncio

from pydapper import connect_async


async def main():
    async with connect_async() as commands:
        rowcount = await commands.execute_async(
            "update task set description = ?desc? where id = ?id?",
            param=[{"desc": "A single update!", "id": 1}, {"desc": "No wait, multiple updates!", "id": 2}],
        )

    print(rowcount)
    # 2


asyncio.run(main())
(This script is complete, it should run "as is")

Example - Execute Delete🔗

Single🔗

Execute the DELETE statement a single time.

import asyncio

from pydapper import connect_async


async def main():
    async with connect_async() as commands:
        rowcount = await commands.execute_async("delete from task where id = ?id?", param={"id": 1})

    print(rowcount)
    # 1


asyncio.run(main())
(This script is complete, it should run "as is")

Multiple🔗

Execute the DELETE statement multiple times, one for each object in the param list.

import asyncio

import pydapper


async def main():
    async with pydapper.connect_async() as commands:
        rowcount = await commands.execute_async("delete from task where id = ?id?", param=[{"id": 2}, {"id": 3}])

    print(rowcount)
    # 2


asyncio.run(main())
(This script is complete, it should run "as is")