python - 雪花: SQL compilation error: error line invalid identifier '"dateutc"'

标签 python sqlalchemy snowflake-cloud-data-platform

我正在将数据从 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/

相关文章:

snowflake-cloud-data-platform - 使用 DBT 更改雪花表数据类型

sqlalchemy - 更新 Snowflake 中的混合和嵌套对象

python - 在 python 中迭代大型 CSV 文件时如何轻松内存?

python - Selenium:等到文本不出现在元素中

python - ADLS Gen2 上的 Kubernetes Spark-submit 错误 : Class org. apache.hadoop.fs.azurebfs.SecureAzureBlobFileSystem 未找到

sqlalchemy - SQLAlchemy 连接的模拟异步上下文管理器

python - 使用日期获取 DataFrame 中的异常值

python - SAWarning 测试失败

python - 我需要使用 pyinstaller 应用程序打包 MySQL 服务器/客户端吗?

apache-spark - 网.snowflake.client.jdbc.SnowflakeSQLException : SQL compilation error: Operation is not supported in reader account