简体   繁体   English

Python Snowflake Connector - 不能使用 set 语句?

[英]Python Snowflake Connector - cannot use set statement?

I get an error trying to use the set statement to create session variables while using python snowflake connector.在使用 python 雪花连接器时尝试使用 set 语句创建会话变量时出现错误。

Is this even possible?这甚至可能吗? Or would we have to create our own string interpolation?还是我们必须创建自己的字符串插值?

You can use set statements with the Snowflake Connector if you use the executemany Python command and the EXECUTE IMMEDIATE Snowflake command.如果您使用executemany Python 命令和EXECUTE IMMEDIATE Snowflake 命令,则可以将set语句与 Snowflake 连接器一起使用。

An example on setting two variables then adding them together would look like:设置两个变量然后将它们相加的示例如下所示:

    queries = [
        ['SET a = 2'],
        ['SET b = 3'],
        ['SELECT %a + %b']
    ]
    
    sql = 'EXECUTE IMMEDIATE %s'
    
    cursor.executemany(sql, queries)
    print(cursor.fetchall())

    #Returns [(5,)]

In this example '%s' is replaced by each of the three strings in 'queries', then those three queries are queued and submitted in one transaction.在此示例中,“%s”被“查询”中的三个字符串中的每一个替换,然后这三个查询被排队并在一个事务中提交。

If you would like to know more about using 'set' in Python-sent SQL queries, EXECUTE IMMEDIATE, and the Python command executemany , follow these links:如果您想了解更多关于在 Python 发送的 SQL 查询、EXECUTE IMMEDIATE 和 Python 命令executemany中使用“set”的信息,请点击以下链接:

http://dot-pi.com/use-set-in-sql-commands-sent-from-python/ http://dot-pi.com/use-set-in-sql-commands-sent-from-python/

http://dot-pi.com/use-pythons-executemany-command-to-run-multiple-sql-queries-at-once/ http://dot-pi.com/use-pythons-executemany-command-to-run-multiple-sql-queries-at-once/

Note: these are web pages I have written注意:这些是我写的网页

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

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