sql 取 7天内的数据:
and create_time >= cast(CURRENT_DATE - 7 as varchar)
出现:
ERROR: operator does not exist: timestamp with time zone >= character varying
LINE 5: and create_time >= cast(CURRENT_DATE - 7 as varchar)
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
异常原因 是不同类型 的 数据进行了对比,需要 把 对比的信息 转换时间格式
and create_time >= cast(CURRENT_DATE - 7 as varchar)::TIMESTAMP
用 pandas 使用 sql 读取数据时 出现
"Excel does not support datetimes with "
ValueError: Excel does not support datetimes with timezones. Please ensure that datetimes are timezone unaware before writing to Excel.
是因为 to_excel 的 时间格式有问题 使用 to_csv 不会有时间报错问题
# 增加这行代码
data1['create_time'] = data1['create_time'].dt.tz_localize(None)
import pandas as pd
import psycopg2
# 建立数据库连接
con = psycopg2.connect(database="***", user="***", password="***", host="***", port="5432")
sql_command = """
select tag, project, create_time, job_url, node_name
from public.query_build_case_result
where node_name = '***-053'
and create_time >= cast(CURRENT_DATE - 60 as varchar)::TIMESTAMP
"""
"""
data1 = pd.read_sql(sql_command, con)
data1['create_time'] = data1['create_time'].dt.tz_localize(None) # 增加这行不会报错
file_path = f'file_0531.xlsx'
data1.to_excel(excel_writer=file_path, sheet_name='sheet_1')
# data1.to_csv(file_path.replace('.xlsx', '.csv')) # 使用这个 可以不用增加.dt.tz_localize(None) 变形操作