SQLExecuteQueryOperator to connect to Sqlite

Use the SQLExecuteQueryOperator to execute Sqlite commands in a Sqlite database.

Warning

Previously, SqliteOperator was used to perform this kind of operation. But at the moment SqliteOperator is deprecated and will be removed in future versions of the provider. Please consider to switch to SQLExecuteQueryOperator as soon as possible.

Using the Operator

Use the conn_id argument to connect to your Sqlite instance where the connection metadata is structured as follows:

Sqlite Airflow Connection Metadata

Parameter

Input

Host: string

Sqlite database file

An example usage of the SQLExecuteQueryOperator to connect to Sqlite is as follows:

tests/system/sqlite/example_sqlite.py[source]


    # Example of creating a task that calls a common CREATE TABLE sql command.
    create_table_sqlite_task = SQLExecuteQueryOperator(
        task_id="create_table_sqlite",
        sql=r"""
        CREATE TABLE Customers (
            customer_id INT PRIMARY KEY,
            first_name TEXT,
            last_name TEXT
        );
        """,
    )

Furthermore, you can use an external file to execute the SQL commands. Script folder must be at the same level as DAG.py file.

tests/system/sqlite/example_sqlite.py[source]


    # Example of creating a task that calls an sql command from an external file.
    external_create_table_sqlite_task = SQLExecuteQueryOperator(
        task_id="create_table_sqlite_external_file",
        sql="create_table.sql",
    )

Reference

For further information, look at:

Note

Parameters given via SQLExecuteQueryOperator() are given first-place priority relative to parameters set via Airflow connection metadata (such as schema, login, password etc).

Was this entry helpful?