使用 SQLExecuteQueryOperator 连接到 Sqlite

使用 SQLExecuteQueryOperatorSqlite 数据库中执行 Sqlite 命令。

注意

之前,SqliteOperator 用于执行此类操作。弃用后已移除。请改用 SQLExecuteQueryOperator

使用算子

使用 conn_id 参数连接到您的 Sqlite 实例,连接元数据结构如下

Sqlite Airflow 连接元数据

参数

输入

主机:字符串

Sqlite 数据库文件

使用 SQLExecuteQueryOperator 连接到 Sqlite 的一个示例如下

tests/system/sqlite/example_sqlite.py


    # 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
        );
        """,
    )

此外,您可以使用外部文件执行 SQL 命令。脚本文件夹必须与 DAG.py 文件处于同一级别。

tests/system/sqlite/example_sqlite.py


    # 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",
    )

参考

欲了解更多信息,请参阅

注意

通过 SQLExecuteQueryOperator() 提供的参数相对于通过 Airflow 连接元数据设置的参数(例如 schemaloginpassword 等)具有优先权。

此条目是否有帮助?