我正在将数据从 Postgres 移动到雪花。最初它有效,但我添加了:
df_postgres["dateutc"]= pd.to_datetime(df_postgres["dateutc"])
因为日期格式未正确加载到雪花,现在我看到此错误:
SQL compilation error: error line 1 at position 87 invalid identifier '"dateutc"'
这是我的代码:
from sqlalchemy import create_engine
import pandas as pd
import glob
import os
from config import postgres_user, postgres_pass, host,port, postgres_db, snow_user, snow_pass,snow_account,snow_warehouse
from snowflake.connector.pandas_tools import pd_writer
from snowflake.sqlalchemy import URL
from sqlalchemy.dialects import registry
registry.register('snowflake', 'snowflake.sqlalchemy', 'dialect')
engine = create_engine(f'postgresql://{postgres_user}:{postgres_pass}@{host}:{port}/{postgres_db}')
conn = engine.connect()
#reads query
df_postgres = pd.read_sql("SELECT * FROM rok.my_table", conn)
#dropping these columns
drop_cols=['RPM', 'RPT']
df_postgres.drop(drop_cols, inplace=True, axis=1)
#changed columns to lowercase
df_postgres.columns = df_postgres.columns.str.lower()
df_postgres["dateutc"]= pd.to_datetime(df_postgres["dateutc"])
print(df_postgres.dateutc.dtype)
sf_conn = create_engine(URL(
account = snow_account,
user = snow_user,
password = snow_pass,
database = 'test',
schema = 'my_schema',
warehouse = 'test',
role = 'test',
))
df_postgres.to_sql(name='my_table',
index = False,
con = sf_conn,
if_exists = 'append',
chunksize = 300,
method = pd_writer)
最佳答案
为了完整性,将 Ilja 的答案从评论移至答案:
- Snowflake 区分大小写。
- 在编写“不带引号的”SQL 时,Snowflake 会将表名和字段转换为大写。
- 这通常是有效的,直到有人决定开始在 SQL 中引用他们的标识符。
pd_writer
为标识符添加引号。- 因此,当您有
df_postgres["dateutc"]
时,它在转换为完全引用的查询时仍保持小写形式。 - 用 Python 编写
df_postgres["DATEUTC"]
应该可以解决该问题。
关于python - 雪花: SQL compilation error: error line invalid identifier '"dateutc"',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67288225/