Prepared Statement
5/27/25
Representation of PostgreSQL PreparedStatement.
Usage
Execute
from psqlpy import ConnectionPool, QueryResult
db_pool: Final = ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/postgres",
)
async def main() -> None:
connection = await db_pool.connection()
prepared_stmt = await connection.prepare(
querystring="SELECT * FROM users WHERE id > $1",
parameters=[100],
)
result: QueryResult = await prepared_stmt.execute()
Cursor
from psqlpy import ConnectionPool, Cursor, PreparedStatement
db_pool: Final = ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/postgres",
)
async def main() -> None:
connection = await db_pool.connection()
prepared_stmt: PreparedStatement = await connection.prepare(
querystring="SELECT * FROM users WHERE id > $1",
parameters=[100],
)
cursor: Cursor = prepared_stmt.cursor()
PreparedStatement methods
Execute
Just execute prepared statement.
async def main() -> None:
connection = await db_pool.connection()
prepared_stmt = await connection.prepare(
querystring="SELECT * FROM users WHERE id > $1",
parameters=[100],
)
result: QueryResult = await prepared_stmt.execute()
Cursor
Create new Cursor instance from the PreparedStatement.
async def main() -> None:
connection = await db_pool.connection()
prepared_stmt = await connection.prepare(
querystring="SELECT * FROM users WHERE id > $1",
parameters=[100],
)
result: QueryResult = await prepared_stmt.execute()