如何为cassandra golang形成动态插入查询

我一直在尝试使用 gocql 驱动程序在 golang 中为 cassandra 创建一个动态查询,这是我到目前为止所尝试的


func WriteRecord(session gocql.Session, insertstring string, table string, fields []string, values ...interface{}) error {

    var placeholder []string


    for range fields {

        placeholder = append(placeholder, "?")

    }

    querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, ", "), strings.Join(placeholder, ", "))

    fmt.Println(querystring)

    return session.Query(querystring, values...).Exec()

}

并在此调用此方法


func writeData(session gocql.Session) {

    fields := []string{

        "id",

        "message",

    }


    for i := 1; i <= 10; i++ {

        /*

            if err := session.Query(

                "INSERT INTO example_keyspace.example_go (id, message) VALUES (?, ?)", i, "Hello from golang!",

            ).Exec(); err != nil {

                log.Fatal(err)

            }

        */

        insertString := "INSERT INTO example_keyspace.%s(%s,%s) VALUES (%s,%s)"

        err := WriteRecord(session, insertString, "kafka", fields, i, "hey kafka")

        if err != nil {

            log.Fatal(err)

        }


    }

}

它给了我这个输出


插入 example_keyspace.kafka(id, message,?, ?) VALUES (%!s(MISSING),%!s(MISSING))


如何解决这个问题,我不确定我哪里做错了


有只小跳蛙
浏览 90回答 1
1回答

长风秋雁

您几乎是对的,只是对格式化的 insertstring 进行了一些小的修改,请参见下文func WriteRecord(session gocql.Session, insertstring string, table string, fields []string, values ...interface{}) error {&nbsp; &nbsp; var placeholder []string&nbsp; &nbsp; for range values {&nbsp; &nbsp; &nbsp; &nbsp; placeholder = append(placeholder, "?")&nbsp; &nbsp; }&nbsp; &nbsp; querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, ", "), strings.Join(placeholder, ", "))&nbsp; &nbsp; fmt.Println(querystring)&nbsp; &nbsp; return session.Query(querystring, values...).Exec()}func writeData(session gocql.Session) {&nbsp; &nbsp; fields := []string{&nbsp; &nbsp; &nbsp; &nbsp; "id",&nbsp; &nbsp; &nbsp; &nbsp; "message",&nbsp; &nbsp; }&nbsp; &nbsp; for i := 1; i <= 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err := session.Query(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "INSERT INTO example_keyspace.example_go (id, message) VALUES (?, ?)", i, "Hello from golang!",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ).Exec(); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; insertString := "INSERT INTO example_keyspace.%s(%s) VALUES (%s)"&nbsp; &nbsp; &nbsp; &nbsp; err := WriteRecord(session, insertString, "kafka", fields, i, "hey kafka") // Just remove extra %s as you are joining the string&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}您将获得的最终 insertstring 输出是INSERT INTO example_keyspace.kafka(id, message) VALUES (?, ?)按照这条线return session.Query(querystring, values...).Exec() // the&nbsp; values will be passed希望能帮助到你
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go