英文:
How to form dynamic insert query for 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++ {
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)
}
}
}
它给我输出了以下内容:
INSERT INTO example_keyspace.kafka(id, message,?, ?) VALUES (%!s(MISSING),%!s(MISSING))
如何解决这个问题,我不确定我做错了什么。
英文:
I have been trying to create a dynamic query in golang for cassandra using gocql driver ,this is what I tried so far
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()
}
And calling this method in this
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)
}
}
}
its giving me this output
> INSERT INTO example_keyspace.kafka(id, message,?, ?) VALUES
> (%!s(MISSING),%!s(MISSING))
How to fix this problem ,I am not sure where I am doing wrong
答案1
得分: 1
你几乎是正确的,只需要对你的格式化插入字符串进行一些小的修改,如下所示:
func WriteRecord(session gocql.Session, insertstring string, table string, fields []string, values ...interface{}) error {
var placeholder []string
for range values {
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) VALUES (%s)"
err := WriteRecord(session, insertString, "kafka", fields, i, "hey kafka") // 只需删除多余的 %s,因为你正在连接字符串
if err != nil {
log.Fatal(err)
}
}
}
你将得到最终的 insertstring 输出为:
INSERT INTO example_keyspace.kafka(id, message) VALUES (?, ?)
根据这行代码:
return session.Query(querystring, values...).Exec() // values 将被传递
希望对你有所帮助。
英文:
You are almost right just small modifications in your formatted insertstring ,see below
func WriteRecord(session gocql.Session, insertstring string, table string, fields []string, values ...interface{}) error {
var placeholder []string
for range values {
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) VALUES (%s)"
err := WriteRecord(session, insertString, "kafka", fields, i, "hey kafka") // Just remove extra %s as you are joining the string
if err != nil {
log.Fatal(err)
}
}
}
the final insertstring output you would get is
INSERT INTO example_keyspace.kafka(id, message) VALUES (?, ?)
And as per this line
return session.Query(querystring, values...).Exec() // the values will be passed
Hope it helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论