How to form dynamic insert query for cassandra golang

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

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, &quot;?&quot;)
	}
	querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, &quot;, &quot;), strings.Join(placeholder, &quot;, &quot;))
	fmt.Println(querystring)
	return session.Query(querystring, values...).Exec()
}

And calling this method in this

func writeData(session gocql.Session) {
	fields := []string{
		&quot;id&quot;,
		&quot;message&quot;,
	}

	for i := 1; i &lt;= 10; i++ {
		/*
			if err := session.Query(
				&quot;INSERT INTO example_keyspace.example_go (id, message) VALUES (?, ?)&quot;, i, &quot;Hello from golang!&quot;,
			).Exec(); err != nil {
				log.Fatal(err)
			}
		*/
		insertString := &quot;INSERT INTO example_keyspace.%s(%s,%s) VALUES (%s,%s)&quot;
		err := WriteRecord(session, insertString, &quot;kafka&quot;, fields, i, &quot;hey kafka&quot;)
		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, &quot;?&quot;)
	}
	querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, &quot;, &quot;), strings.Join(placeholder, &quot;, &quot;))
	fmt.Println(querystring)
	return session.Query(querystring, values...).Exec()
}



func writeData(session gocql.Session) {
	fields := []string{
		&quot;id&quot;,
		&quot;message&quot;,
	}

	for i := 1; i &lt;= 10; i++ {
		/*
			if err := session.Query(
				&quot;INSERT INTO example_keyspace.example_go (id, message) VALUES (?, ?)&quot;, i, &quot;Hello from golang!&quot;,
			).Exec(); err != nil {
				log.Fatal(err)
			}
		*/
		insertString := &quot;INSERT INTO example_keyspace.%s(%s) VALUES (%s)&quot;
		err := WriteRecord(session, insertString, &quot;kafka&quot;, fields, i, &quot;hey kafka&quot;) // 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

huangapple
  • 本文由 发表于 2022年5月10日 12:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/72180923.html
匿名

发表评论

匿名网友

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

确定