简体   繁体   English

使用 Snowflake python 连接器从 pandas 添加然后查询临时表

[英]Add and then query temp table from pandas with Snowflake python connector

I am trying to create a temporary table from a pandas df and then use it in a sql statement我正在尝试从 pandas df 创建一个临时表,然后在 sql 语句中使用它

import snowflake.connector
from snowflake.connector.pandas_tools import write_pandas

with snowflake.connector.connect(
account='snoflakewebsite',
user='username',
authenticator='externalbrowser',
database='db',
schema='schema'
) as con:

    success, nchunks, nrows, _ = write_pandas(
        conn=con,                    
        df=df,
        table_name='temp_table',
        auto_create_table = True,
        table_type='temporary',
        overwrite = True,
        database='db',
        schema='schema' 
       )

    cur = con.cursor()
    cur.execute('select * from temp_table')    

The error I get:我得到的错误:

ProgrammingError: 002003 (42S02): SQL compilation error: Object 'TEMP_TABLE' does not exist or not authorized. ProgrammingError: 002003 (42S02): SQL 编译错误:Object 'TEMP_TABLE' 不存在或未授权。

write_pandas() creates a table using the letter case exactly how it is passed in table_name= , while the query submitted in cur.execute() passes the entire string with the query to Snowflake SQL, and Snowflake SQL capitalizes the object names unless they are written in double quotes. write_pandas()使用字母大小写创建一个表,完全按照它在table_name=中传递的方式,而在cur.execute()中提交的查询将整个字符串和查询一起传递给 Snowflake SQL,Snowflake SQL 将 object 名称大写,除非它们是用双引号写的。

Therefore, either you create a table using capital letters table_name='TEMP_TABLE', or you query it using double quotes:因此,要么使用大写字母table_name='TEMP_TABLE',创建表,要么使用双引号查询它:

  cur.execute('select * from "temp_table"')

In this case, you will get your table created in small letters, and you always need to add double quotes to refer to its name.在这种情况下,您将以小写字母创建表格,并且始终需要添加双引号来引用其名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM