python - 使用雪花连接器和熊猫在查询中传递变量
问题描述
我正在尝试在 python 中参数化雪花查询,但它不起作用。这是代码:
ctx = snowflake.connector.connect(user=user, password=password, account=xxx)
cs = ctx.cursor()
cs.execute("use role {};".format(SNOWFLAKE_ROLE))
cs.execute("use warehouse {}; ".format(SNOWFLAKE_WAREHOUSE))
cs.execute("use database {};".format(database))
cs.execute("use schema {};".format(schema))
cs.execute("alter warehouse {} resume if suspended;".format(SNOWFLAKE_WAREHOUSE))
id=500000
query="""
create or replace temp table test as
select *
from tablename as a
where acct_id =%id;
"""
df1 = pandas.read_sql_query(query, ctx,params={id})
print(df1)
我得到的错误是“:必须是实数,而不是 str”。我不确定为什么会出现此错误。是否有更好的方法在查询中传递变量?
解决方案
找到了一个可行的解决方案:
query="""
create or replace temp table test as
select *
from tablename as a
where acct_id ={id};
"""
df1 = pandas.read_sql_query(query, ctx,params={'acct_id':id})
推荐阅读
- python - Pandas - 比较 2 个数据框并找到最接近的值
- file-upload - 将图像发送到前端的最有效方式
- java - Java Library Commons Lang3 'ClassNotFoundException' 错误
- javascript - 从 gs 传递一个对象作为 js 中的全局变量
- c# - 何时在具有存储库模式的 .net core web-api 上使用 async/await?
- postgresql - PHP 中的安全 SQL 更新
- cassandra - Cassandra在现有集群中添加节点和修改snitch
- sharepoint-online - Power Automate 在 Microsft Teams 中创建一个频道并让所有团队成员都可以看到
- windows - 无法使用 powershell 从映射的网络驱动器复制项目,因为文件包含奇怪的字符
- algorithm - 从运行时分析推导出时间复杂度