将一个映射作为值传递以插入到Cassandra中。

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

Passing a map as a value to insert into Cassandra

问题

我正在尝试将一个地图值插入到我的Cassandra数据库中。我正在使用Go语言编写客户端。目前它抛出了错误"无法将字符串编组为map(varchar, varchar)"。我理解这个错误是什么意思,但我无法解决它。以下是我编写的代码。

if err := session.Query("INSERT INTO emergency_records
		(mapColumn)
		VALUES (?)",
		"{'key' : 'value'}").Exec();
		 err != nil {
			log.Fatal(err)
		}

我不明白的是,我已经将一个查询作为一个完整的字符串编写,并且它可以正常工作而不抛出这个错误。然而,如果将它拆分成带有问号的形式,就会抛出错误。我知道这是一个简单的问题,我可能只是忽略了它,并且在文档中找不到答案,但任何帮助都将是非常感谢的。

英文:

I'm trying to insert a map value into my Cassandra database. I'm using Go to write my client. Currently its throwing the error "can not marshal string into map(varchar, varchar)". I understand what the error is, but I can't resolve it. Here is the code that I've written.

if err := session.Query("INSERT INTO emergency_records
		(mapColumn)
		VALUES (?)",
		"{'key' : 'value'}").Exec();
		 err != nil {
			log.Fatal(err)
		}

What I don't get is that I've written one query as a whole unbroken string and it works fine without throwing this error. Yet breaking it down with the question mark it throws the error. I know this is something simple that I'm just overlooking and couldn't find in the documentation, but any help would be great thanks.

答案1

得分: 2

我以前没有使用过Go Cassandra客户端,但我猜想将map作为map传递而不是字符串应该可以工作:

mapValue := map[string]string{"key": "value"}
if err := session.Query("INSERT INTO emergency_records (mapColumn) VALUES (?)", mapValue).Exec(); err != nil {
	log.Fatal(err)
}
英文:

I haven't used Go casandra client before but I guess passing map as a map instead of string should work:

mapValue := map[string]string{"key": "value"}
if err := session.Query("INSERT INTO emergency_records (mapColumn) VALUES (?)", mapValue).Exec(); err != nil {
	log.Fatal(err)
}

huangapple
  • 本文由 发表于 2016年2月15日 12:05:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/35401344.html
匿名

发表评论

匿名网友

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

确定