dongxiong2000 2014-11-10 16:07
浏览 78

如果gocql中不存在,如何进行插入

I've been reading http://godoc.org/github.com/gocql/gocql Yet I don't understand how to do INSERT -- IF NOT EXIST with gocql.
It stated that

func (*Query) ScanCAS

func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error)

ScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT statement containing an IF clause). If the transaction fails because the existing values did not match, the previous values will be stored in dest.

When I run

cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "example"
cluster.Consistency = gocql.Quorum
session, _ := cluster.CreateSession()
defer session.Close()

var mapQ map[string]interface{}
var inserted bool
var id gocql.UUID
var timeline, text string
// insert a tweet
isTrue, err := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?) IF NOT EXIST`,
    "hermano", gocql.TimeUUID(), "good night").
    ScanCAS(); if err != nil {
    log.Println(err)
}
fmt.Println(timeline, id, text)
fmt.Printf("%+v
", isTrue)
fmt.Printf("%+v
", inserted)
fmt.Printf("%+v
", mapQ)

I get:
Failed parsing statement: [INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?) IF NOT EXIST] reason: ArrayIndexOutOfBoundsException -1

So my question is:
1. How to actually do INSERT IF NOT EXIST in gocql? Can you guys give me any example?
2. How to do proper ScanCAS?
3. What makes MapScanCAS and ScanCAS different? I don't understand what column mismatching are the author is talking about
4. Are there any good page that explains gocql beside its godoc page?

  • 写回答

2条回答

  • duanhan3067 2016-02-03 04:35
    关注

    looks like a typo, you should use "IF NOT EXISTS"

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题