PythonSnowflake连接器抛出无效标识符None

2024-04-29 07:15:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,其中一部分在查询中调用Snowflake。该查询位于.sql文件夹中,并在脚本中向其传递两个参数。例如:

#query.sql
select *
from {some_table}
where
date == '{hit_date}'
and 
id in ({id_list}) or ({id_list}) is null)

这段脚本的作用如下:

def run_query(hit_date, id_list):
    conn = snowflake.connector.connect(**snowflake_creds)
    cursor = conn.cursor()
    
    
    with open(query.sql) as f:
        query = f.read()
    cursor.execute(query.format(hit_date, id_list))

当“id_list”的输入为“None”时,就会出现问题。我被抛出这个错误:

snowflake.connector.errors.ProgrammingError: 000904 (42000): 019b2591-04da-7b13-0000-89bd25e0043a: SQL compilation error: error line 20 at position 42 invalid identifier 'NONE'

据我所知,Python的'None'将自动转换为SQL的'Null'。不是这样吗?如果“无/空”,则“id_列表”的输入应该是什么


Tags: 脚本noneidsqlconnectordateerrorconn
1条回答
网友
1楼 · 发布于 2024-04-29 07:15:02

我找不到这个具体错误的明确答案。然而,当没有给出列表时,我可以通过用一个不可能的值替换条件来解决这个问题

#query.sql
select *
from {some_table}
where
date == '{hit_date}'
and 
(id in ({id_list}) or impossible_value in ({id_list}))

相关问题 更多 >