SQLExecuteQueryOperator 連接 Sqlite¶
使用 SQLExecuteQueryOperator
在 Sqlite 資料庫中執行 Sqlite 命令。
注意
先前,SqliteOperator
用於執行此類操作。在棄用後,此運算子已被移除。請改用 SQLExecuteQueryOperator
。
使用運算子¶
使用 conn_id
參數來連接到您的 Sqlite 實例,其連線中繼資料結構如下
參數 |
輸入 |
---|---|
主機:字串 |
Sqlite 資料庫檔案 |
以下是 SQLExecuteQueryOperator 連接 Sqlite 的範例用法
# 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 檔案位於同一層級。
# 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",
)