python - 如何将条件传递给游标中的 PostgreSQL 查询
问题描述
我正在尝试将条件传递给查询,并且在 apache 气流中使用 PostgreSQLHook。当我运行 dag 时出现错误
TypeError:“日期时间”类型的对象不是 JSON 可序列化的
目前我将我的 sql 存储在一个不同的文件中,并且我正在导入该文件以在我的 python 文件中执行。
sql pg_query文件
SELECT
*
FROM
public.airtimes
WHERE created_at > '{}';
蟒蛇脚本
def read_latest_data_from_pg(**kwargs):
with open('dags/scripts/sql_scripts/pg_query.sql','r') as sqlfile:
pg_export_data_query=str(sqlfile.read())
pg_date = '2021-05-01'
pg_hook = PostgresHook(postgres_conn_id='pg_conn', delegate_to=None, use_legacy_sql=False)
conn = pg_hook.get_conn()
cursor = conn.cursor()
cursor.execute(pg_export_data_query.format(pg_date))
result = cursor.fetchall()
print('result', result)
return result
我错过了什么?
解决方案
推荐阅读
- react-native - 有没有一种好方法可以设置 headerRight 以在 React Native 中传递 onPress 事件
- ios - 从 SwiftUI 按钮从另一个 swift 文件调用函数不听从函数返回
- javascript - 如何使用 axios 捕获 http json 响应并将值传递给另一个函数?
- verilog - SystemVerilog 连接分配不正确
- c++ - Bazel 链接器未找到函数定义
- appium - 如何让 Webdriverio 与 appium-desktop 检查器一起使用?
- webstorm - 如何在 WebStorm 控制台中禁用换行?
- asp.net-core - NavigationManager.Uri 根本不会更新,除非页面被刷新。如何在 Blazor Server 中获取真实的当前 URI?
- python - Django 模型:从两个选项中选择一个 FK(只有一个)
- java - Selenium Java Lib 未在 Gradle 中导入