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 等)。

這個條目有幫助嗎?