Skip to main content

prefect-sqlalchemy

The prefect-sqlalchemy helps you connect to a database in your Prefect flows.

Getting Started

Prerequisites

Install prefect-sqlalchemy

Install or update to the latest version of the prefect-sqlalchemy library and dependencies.

Register newly installed block types

Register the block types in the prefect-sqlalchemy module to make them available for use.

Examples

Save credentials to a block

To use the load method on Blocks, you must have a block saved through code or saved through the UI.
Load the saved block that holds your credentials:
The required arguments depend upon the desired driver. For example, SQLite requires only the driver and database arguments:

Work with databases in a flow

To set up a table, use the execute and execute_many methods. Use the fetch_many method to retrieve data in a stream until there’s no more data. Use the SqlAlchemyConnector as a context manager, to ensure that the SQLAlchemy engine and any connected resources are closed properly after you’re done with them.
Async supportSqlAlchemyConnector supports async workflows. Just be sure to save, load, and use an async driver, as in the example below.

Resources

Refer to the prefect-sqlalchemy SDK documentation linked in the sidebar to explore all the capabilities of the prefect-sqlalchemy library. For assistance using SQLAlchemy, consult the SQLAlchemy documentation.