Apache Airflow SQLite Provider

4.3.1 · active · verified Thu Apr 09

The `apache-airflow-providers-sqlite` package provides the necessary components to interact with SQLite databases within Apache Airflow DAGs. It includes the `SqliteHook` for programmatic access and `SqliteOperator` for defining tasks. This provider is currently at version 4.3.1 and typically releases alongside major Apache Airflow provider updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Airflow DAG using the `SqliteOperator` to create a table, insert data, and query it. Ensure an Airflow connection named `sqlite_default` is configured, or it will default to an in-memory database. For a persistent database file, specify the path in the 'Host' field of the connection or in the 'Extra' field (e.g., `{"database":"/path/to/my.db"}`).

from __future__ import annotations
import pendulum
from airflow.models.dag import DAG
from airflow.providers.sqlite.operators.sqlite import SqliteOperator

with DAG(
    dag_id="sqlite_example_dag",
    start_date=pendulum.datetime(2023, 1, 1, tz="UTC"),
    schedule=None,
    catchup=False,
    tags=["sqlite", "example"],
) as dag:
    create_table = SqliteOperator(
        task_id="create_table",
        sqlite_conn_id="sqlite_default",
        sql="""
            CREATE TABLE IF NOT EXISTS test_table (
                id INTEGER PRIMARY KEY,
                name TEXT
            );
        """,
    )

    insert_data = SqliteOperator(
        task_id="insert_data",
        sqlite_conn_id="sqlite_default",
        sql="INSERT INTO test_table (name) VALUES ('Airflow'), ('SQLite');",
    )

    query_data = SqliteOperator(
        task_id="query_data",
        sqlite_conn_id="sqlite_default",
        sql="SELECT * FROM test_table;",
        handler=lambda x: [print(row) for row in x] # Example handler to print results to logs
    )

    create_table >> insert_data >> query_data

view raw JSON →