使用Go中的Snowflake驱动程序创建预编译语句

huangapple go评论88阅读模式
英文:

Creating prepared statements with Snowflake driver in Go

问题

我正在尝试使用Go的Snowflake驱动程序创建预编译语句:https://github.com/snowflakedb/gosnowflake

然而,每次我尝试准备一个需要字符串的语句时,它都无法将它们绑定到整个查询。

这是一个示例:

import (
    "database/sql"
    _ "github.com/snowflakedb/gosnowflake"
)

func getData(db *sql.DB) error {

    tbl := "DATASET"
    s := "Bob"

    stmt := "INSERT INTO ? SELECT * FROM OTHER_TBL WHERE name = ?"

    _, err := db.Exec(stmt, dataset, s)
    if err != nil {
        return err
    }
    return nil
}

查询总是失败,并显示语法错误,指出它无法识别?。我也尝试了.Prepare()方法,结果相同。

我还尝试了对?占位符使用单引号,但在我的实际查询中,我有几个?,它们都没有被绑定。

我只有在使用fmt.Sprintf()方法时才能成功,但我想避免使用该方法。

英文:

I am trying to create prepared statements using the Snowflake driver for Go: https://github.com/snowflakedb/gosnowflake

However, every time I try to prepare a statement that requires strings, it fails to bind them to the overall query.

Here is an example:

import (
    "database/sql"
    _ "github.com/snowflakedb/gosnowflake"
)

func getData(db *sql.DB) error {

    tbl := "DATASET"
    s := "Bob"

    stmt := "INSERT INTO ? SELECT * FROM OTHER_TBL WHERE name = ?"

    _, err := db.Exec(stmt, dataset, s)
    if err != nil {
        return err
    }
    return nil
}

The query always fails with a Syntax error stating it didn't recognize the ?. I've also tried the .Prepare() method and I get the same result.

I've also tried single quoting the ? placeholder, but in my actual query, I have several ? and it doesn't bind any of them.

I'm left with only having success if I use fmt.Sprintf() but I'd like to avoid that method.

答案1

得分: 1

在你的代码片段中,你对表名进行了参数化。

Snowflake实现了database/sql接口。根据我对这个库的经验,不允许对表名或列名进行参数化。

英文:

In your snippet code, you are parameterized the table name.

Snowflake implements database/sql interfaces. as my experience with this library, parameterization table or column names are not allowed.

huangapple
  • 本文由 发表于 2023年2月15日 06:03:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75453671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定