如何在Go SDK中使用Couchbase N1QL查询扩展

在golang Couchbase SDK中,有N1QL选择示例,其中记录了以下示例:</ p>

  myQuery:= gocb.NewN1qlQuery(“从“旅行样本”中选择机场名称,城市,国家/地区“ + 
” WHERE type ='airport'AND city = $ 1“)
myParams = append(myParams, [] interface {} {“ Reno”})
rows,错误:= bucket.ExecuteN1qlQuery(myQuery,myParams)
</ code> </ pre>

当我尝试以下操作时, </ p>

  query:= gocb.NewN1qlQuery(“ SELECT * FROMbucket WHERE META()。id like'TD:$ 1:%'  “)

rows,错误:= r.Bucket.ExecuteN1qlQuery(query,[] interface {} {userid})
if err!= nil {
return nil,err
}
</ code> < / pre>

但这确实有效,并且也可以在Couchbase控制台中使用:</ p>

  query:= gocb.NewN1qlQuery(“ SELECT * FROMbucket  WHERE META()。id类似于'TD:“ + userid +”:%'“)
rows,err:= bucket.ExecuteN1qlQuery(query,nil)
</ code> </ pre>

我是否在做任何明显的工作 在这里?</ p>

虽然非参数化选项有效,但我想使用参数化的选项并将其标记为带有query.AdHoc(false)的准备好的语句</ p>
</ DIV>

展开原文

原文

In the golang Couchbase SDK there are N1QL select examples documented with examples like this:

myQuery := gocb.NewN1qlQuery("SELECT airportname, city, country FROM `travel-sample` " +
        "WHERE type='airport' AND city=$1 ")
myParams = append(myParams, []interface{}{"Reno"})
rows, err := bucket.ExecuteN1qlQuery(myQuery, myParams)

When I try it with the following, it doesn't find any records.

query := gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:$1:%'")

rows, err := r.Bucket.ExecuteN1qlQuery(query, []interface{}{userid})
if err != nil {
    return nil, err
}

But this does work and also works in the Couchbase console:

query := gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:"+userid+":%'")
rows, err := bucket.ExecuteN1qlQuery(query, nil)

Am I doing anything obviously wrong here?

While the non-parametised option works I'd like to use the parametised one and mark it as a prepared statement with query.AdHoc(false)

dqjfw40446
dqjfw40446 查看客户端库的代码,似乎替换发生在服务器上而不是客户端库内部。有道理。在替换位置我可能会有所限制。是这样吗?
大约一年之前 回复

1个回答



  gocb.NewN1qlQuery(“ SELECT * FROMbucket WHERE META()。id Like'TD:$ 1:%'  “)
</ code> </ pre>

在上面的代码中,您的查询参数包含在引号内。 字符串中的查询参数将不会被替换。 因此它正在寻找文档ID“ TD:$ 1:” </ p>

您应该尝试使用此</ p>

  gocb.NewN1qlQuery(“ SELECT * FROM  bucket WHERE META()。id类似于'TD:'|| $ 1 ||':%'“)
</ code> </ pre>

OR </ p>
\ n

  gocb.NewN1qlQuery(“ SELECT * FROMbucket WHERE META()。id LIKE $ 1”)
传递$ 1'TD:'+ userid +':%'
</ code> </ pre>

OR </ p>

  gocb.NewN1qlQuery(“ SELECT * FROMbucket WHERE META()。id LIKE'TD:” + userid +“:  %'“)
</ code> </ pre>
</ div>

展开原文

原文

gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:$1:%'")

In above code your query parameter is include inside the quotes. query parameters inside string will not be replaced. So it is looking for document id "TD:$1:"

You should try this

gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:' || $1 || ':%' ")

OR

   gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE $1")
    Pass $1  'TD:'+userid+':%' 

OR

gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:"+userid+":%'")

doz95923
doz95923 谢谢,当我回到项目时会尝试那些。 我曾尝试不加引号,但一无所获。 所以我认为这需要他们。 第二种选择可能会起作用,所以我会尝试的。
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问