1

我正在尝试将我从 json 响应创建的 df 提取到现有表中(该表当前为空,因为我似乎无法让它工作)

df 如下表所示:

指数 clicks_affiliated
0 3214
1 2221

但我看到以下错误:

snowflake.connector.errors.ProgrammingError: 000904 (42000): SQL 编译错误: 位置 94 处的错误行 1 无效标识符 '"clicks_affiliated"'

雪花中的列名与我的数据框中的列匹配。

这是我的代码:

import pandas as pd
from snowflake.sqlalchemy import URL
from sqlalchemy import create_engine
import snowflake.connector
from snowflake.connector.pandas_tools import write_pandas, pd_writer
from pandas import json_normalize
import requests

  
df_norm = json_normalize(json_response, 'reports')

#I've tried also adding the below line (and removing it) but I see the same error
df = df_norm.reset_index(drop=True)

   
def create_db_engine(db_name, schema_name):
    engine = URL(
        account="ab12345.us-west-2",
        user="my_user",
        password="my_pw",
        database="DB",
        schema="PUBLIC",
        warehouse="WH1",
        role="DEV"
    )
    return engine


def create_table(out_df, table_name, idx=False):
    url = create_db_engine(db_name="DB", schema_name="PUBLIC")
    engine = create_engine(url)
    connection = engine.connect()

    try:
        out_df.to_sql(
            table_name, connection, if_exists="append", index=idx, method=pd_writer
        )

    except ConnectionError:
        print("Unable to connect to database!")

    finally:
        connection.close()
        engine.dispose()

    return True

print(df.head)

create_table(df, "reporting")
4

1 回答 1

0

所以......事实证明我需要将我的数据框中的列更改为大写

我在创建数据框之后添加了这个,它起作用了:

df.columns = map(lambda x: str(x).upper(), df.columns)
于 2021-10-29T18:33:38.340 回答