Package: prefect-sqlalchemy
prefect-sqlalchemyVisit the full docshereto see additional examples and the API reference.The prefect-sqlalchemy collection makes it easy to connect to a database in your Prefect flows. Check out the examples below to get started!Getting StartedIntegrate with Prefect flowsPrefect and SQLAlchemy are a data powerhouse duo. With Prefect, your data pipelines are always on track, and with SQLAlchemy, your databases are a snap to handle! Get ready to experience the ultimate data "flow-chemistry"!To set up a table, use theexecuteandexecute_manymethods. Then, use thefetch_manymethod to retrieve data in a stream until there's no more data.By using theSqlAlchemyConnectoras a context manager, you can make sure that the SQLAlchemy engine and any connected resources are closed properly after you're done with them.Be sure to installprefect-sqlalchemyandsave to blockto run the examples below!!!! note "Async support"`SqlAlchemyConnector` also supports async workflows! Just be sure to save, load, and use an async driver.
```python
from prefect_sqlalchemy import SqlAlchemyConnector, ConnectionComponents, AsyncDriver

connector = SqlAlchemyConnector(
    connection_info=ConnectionComponents(
        driver=AsyncDriver.SQLITE_AIOSQLITE,
        database="DATABASE-PLACEHOLDER.db"
    )
)

connector.save("BLOCK_NAME-PLACEHOLDER")
```=== "Sync"```python
from prefect import flow, task
from prefect_sqlalchemy import SqlAlchemyConnector


@task
def setup_table(block_name: str) -> None:
    with SqlAlchemyConnector.load(block_name) as connector:
        connector.execute(
            "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);"
        )
        connector.execute(
            "INSERT INTO customers (name, address) VALUES (:name, :address);",
            parameters={"name": "Marvin", "address": "Highway 42"},
        )
        connector.execute_many(
            "INSERT INTO customers (name, address) VALUES (:name, :address);",
            seq_of_parameters=[
                {"name": "Ford", "address": "Highway 42"},
                {"name": "Unknown", "address": "Highway 42"},
            ],
        )

@task
def fetch_data(block_name: str) -> list:
    all_rows = []
    with SqlAlchemyConnector.load(block_name) as connector:
        while True:
            # Repeated fetch* calls using the same operation will
            # skip re-executing and instead return the next set of results
            new_rows = connector.fetch_many("SELECT * FROM customers", size=2)
            if len(new_rows) == 0:
                break
            all_rows.append(new_rows)
    return all_rows

@flow
def sqlalchemy_flow(block_name: str) -> list:
    setup_table(block_name)
    all_rows = fetch_data(block_name)
    return all_rows


sqlalchemy_flow("BLOCK-NAME-PLACEHOLDER")
```=== "Async"```python
from prefect import flow, task
from prefect_sqlalchemy import SqlAlchemyConnector
import asyncio

@task
async def setup_table(block_name: str) -> None:
    async with await SqlAlchemyConnector.load(block_name) as connector:
        await connector.execute(
            "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);"
        )
        await connector.execute(
            "INSERT INTO customers (name, address) VALUES (:name, :address);",
            parameters={"name": "Marvin", "address": "Highway 42"},
        )
        await connector.execute_many(
            "INSERT INTO customers (name, address) VALUES (:name, :address);",
            seq_of_parameters=[
                {"name": "Ford", "address": "Highway 42"},
                {"name": "Unknown", "address": "Highway 42"},
            ],
        )

@task
async def fetch_data(block_name: str) -> list:
    all_rows = []
    async with SqlAlchemyConnector.load(block_name) as connector:
        while True:
            # Repeated fetch* calls using the same operation will
            # skip re-executing and instead return the next set of results
            new_rows = await connector.fetch_many("SELECT * FROM customers", size=2)
            if len(new_rows) == 0:
                break
            all_rows.append(new_rows)
    return all_rows

@flow
async def sqlalchemy_flow(block_name: str) -> list:
    await setup_table(block_name)
    all_rows = await fetch_data(block_name)
    return all_rows


asyncio.run(sqlalchemy_flow("BLOCK-NAME-PLACEHOLDER"))
```ResourcesFor more tips on how to use tasks and flows in a Collection, check outUsing Collections!InstallationInstallprefect-sqlalchemywithpip:pipinstallprefect-sqlalchemyRequires an installation of Python 3.7+.We recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.These tasks are designed to work with Prefect 2.0. For more information about how to use Prefect, please refer to thePrefect documentation.Saving Credentials to BlockTo use theloadmethod on Blocks, you must already have a block documentsaved through codeorsaved through the UI.Below is a walkthrough on saving block documents through code; simply create a short script, replacing the placeholders.fromprefect_sqlalchemyimportSqlAlchemyConnector,ConnectionComponents,SyncDriverconnector=SqlAlchemyConnector(connection_info=ConnectionComponents(driver=SyncDriver.POSTGRESQL_PSYCOPG2,username="USERNAME-PLACEHOLDER",password="PASSWORD-PLACEHOLDER",host="localhost",port=5432,database="DATABASE-PLACEHOLDER",))connector.save("BLOCK_NAME-PLACEHOLDER")Congrats! You can now easily load the saved block, which holds your credentials:fromprefect_sqlalchemyimportSqlAlchemyConnectorSqlAlchemyConnector.load("BLOCK_NAME-PLACEHOLDER")The required keywords depend on the desireddriver. For example, sqlite only requires driver and database specified:fromprefect_sqlalchemyimportSqlAlchemyConnector,ConnectionComponents,SyncDriverconnector=SqlAlchemyConnector(connection_info=ConnectionComponents(driver=SyncDriver.SQLITE_PYSQLITE,database="DATABASE-PLACEHOLDER.db"))connector.save("BLOCK_NAME-PLACEHOLDER")!!! info "Registering blocks"Register blocks in this module to
[view and edit them](https://orion-docs.prefect.io/ui/blocks/)
on Prefect Cloud:

```bash
prefect block register -m prefect_sqlalchemy
```A list of available blocks inprefect-sqlalchemyand their setup instructions can be foundhere.FeedbackIf you encounter any bugs while usingprefect-sqlalchemy, feel free to open an issue in theprefect-sqlalchemyrepository.If you have any questions or issues while usingprefect-sqlalchemy, you can find help in either thePrefect Discourse forumor thePrefect Slack community.Feel free to star or watchprefect-sqlalchemyfor updates too!ContributingIf you'd like to help contribute to fix an issue or add a feature toprefect-sqlalchemy, pleasepropose changes through a pull request from a fork of the repository.Here are the steps:Fork the repositoryClone the forked repositoryInstall the repository and its dependencies:pip install -e ".[dev]"Make desired changesAdd testsInsert an entry toCHANGELOG.mdInstallpre-committo perform quality checks prior to commit:pre-commit installgit commit,git push, and create a pull request