Skip to main content

Prefect integrations for interacting with SQLAlchemy.

Project description

prefect-sqlalchemy

Welcome!

Prefect integrations for interacting with various databases.

PyPI

Getting Started

Python setup

Requires 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 the Prefect documentation.

Installation

Install prefect-sqlalchemy with pip:

pip install prefect-sqlalchemy

Then, register to view the block on Prefect Cloud:

prefect block register -m prefect_sqlalchemy

Note, to use the load method on Blocks, you must already have a block document saved through code or saved through the UI.

Write and run a flow

Using a SyncDriver with SqlAlchemyConnector

Use SqlAlchemyConnector as a context manager to execute and execute_many operations; then, fetch_many and fetch_one operations.

from prefect_sqlalchemy import SqlAlchemyConnector, SyncDriver, ConnectionComponents

with SqlAlchemyConnector(
    connection_info=ConnectionComponents(
        driver=SyncDriver.SQLITE_PYSQLITE,
        database="my.db"
    ),
) as database_credentials:
    database_credentials.execute(
        "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);"
    )
    database_credentials.execute(
        "INSERT INTO customers (name, address) VALUES (:name, :address);",
        parameters={"name": "Marvin", "address": "Highway 42"},
    )
    database_credentials.execute_many(
        "INSERT INTO customers (name, address) VALUES (:name, :address);",
        seq_of_parameters=[
            {"name": "Ford", "address": "Highway 42"},
            {"name": "Unknown", "address": "Highway 42"},
        ],
    )
    # Repeated fetch* calls using the same operation will skip re-executing and instead return the next set of results
    print(database_credentials.fetch_many("SELECT * FROM customers", size=2))
    print(database_credentials.fetch_one("SELECT * FROM customers"))

Using an AsyncDriver with SqlAlchemyConnector

Use SqlAlchemyConnector as an async context manager to execute and execute_many operations; then, fetch_many and fetch_one operations.

from prefect_sqlalchemy import SqlAlchemyConnector, AsyncDriver, ConnectionComponents

async with SqlAlchemyConnector(
    connection_info=ConnectionComponents(
        driver=AsyncDriver.SQLITE_AIOSQLITE,
        database="test.db"
    ),
) as database_credentials:
    await database_credentials.execute(
        "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);"
    )
    await database_credentials.execute(
        "INSERT INTO customers (name, address) VALUES (:name, :address);",
        parameters={"name": "Marvin", "address": "Highway 42"},
    )
    await database_credentials.execute_many(
        "INSERT INTO customers (name, address) VALUES (:name, :address);",
        seq_of_parameters=[
            {"name": "Ford", "address": "Highway 42"},
            {"name": "Unknown", "address": "Highway 42"},
        ],
    )
    # Repeated fetch* calls using the same operation will skip re-executing and instead return the next set of results
    print(await database_credentials.fetch_many("SELECT * FROM customers", size=2))
    print(await database_credentials.fetch_one("SELECT * FROM customers"))

Resources

If you encounter any bugs while using prefect-sqlalchemy, feel free to open an issue in the prefect-sqlalchemy repository.

If you have any questions or issues while using prefect-sqlalchemy, you can find help in either the Prefect Discourse forum or the Prefect Slack community.

Feel free to ⭐️ or watch prefect-sqlalchemy for updates too!

Development

If you'd like to install a version of prefect-sqlalchemy for development, clone the repository and perform an editable install with pip:

git clone https://github.com/PrefectHQ/prefect-sqlalchemy.git

cd prefect-sqlalchemy/

pip install -e ".[dev]"

# Install linting pre-commit hooks
pre-commit install

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prefect-sqlalchemy-0.2.2.tar.gz (35.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prefect_sqlalchemy-0.2.2-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file prefect-sqlalchemy-0.2.2.tar.gz.

File metadata

  • Download URL: prefect-sqlalchemy-0.2.2.tar.gz
  • Upload date:
  • Size: 35.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for prefect-sqlalchemy-0.2.2.tar.gz
Algorithm Hash digest
SHA256 3a0d5d181a649a881fefe1ac5d898f6722038a63df6d89bf103655e063d6a486
MD5 50fee0e49d09203686f1d47fd48f63da
BLAKE2b-256 3071d9a6547a76c73c69253cd5fbaa931421a0c736ccdc09f75dbc42e7c647e5

See more details on using hashes here.

File details

Details for the file prefect_sqlalchemy-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for prefect_sqlalchemy-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 31e62beed99dd49604f0f81b0f624f4b478715cb4c0d194f890ace323d56fd07
MD5 ca9dac4093bec2008c47fb1368f61981
BLAKE2b-256 460c2ca4cc9572f36a56de0bec1f132314ad454c9d7e6595cd16ebd09530675b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page