golang Couchbase n1ql query pass params to?

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

golang Couchbase n1ql query pass params to?

问题

我正在尝试找到一种方法来传递参数给查询,但不太确定如何操作。网站上的API看起来有点过时了。

myQuery := gocb.NewN1qlQuery("SELECT * FROM default")
rows, err := myBucket.ExecuteN1qlQuery(myQuery)
if err != nil {
    fmt.Printf("N1QL查询错误:%s\n", err)
}
var row interface{}
for rows.Next(&row) {
    fmt.Printf("行:%+v\n", row)
}
if err := rows.Close(); err != nil {
    fmt.Printf("N1QL查询错误:%s\n", err)
}

因为实际上ExecuteN1qlQuery接受两个参数:

func (b *Bucket) ExecuteN1qlQuery(q *N1qlQuery, params interface{}) (ViewResults, error)

我不确定如何使用它...就像我想创建一个带有占位符的查询,并通过params参数传递值给ExecuteN1qlQuery。就像使用SQL(prepare -> execute)一样。例如:

myQuery := gocb.NewN1qlQuery("SELECT * FROM default WHERE a=? AND b=?")
rows, err := myBucket.ExecuteN1qlQuery(myQuery, []string{"b", "c"})
if err != nil {
    fmt.Printf("N1QL查询错误:%s\n", err)
}
var row interface{}
for rows.Next(&row) {
    fmt.Printf("行:%+v\n", row)
}
if err := rows.Close(); err != nil {
    fmt.Printf("N1QL查询错误:%s\n", err)
}
英文:

I'm trying to find a way to pass parameters to query, but not quite sure how. The API on the web site looks a little bit outdated?

myQuery := gocb.NewN1qlQuery("SELECT * FROM default")
rows, err := myBucket.ExecuteN1qlQuery(myQuery)
if err != nil {
	fmt.Printf("N1QL query error: %s\n", err)
}
var row interface{}
for rows.Next(&row) {
	fmt.Printf("Row: %+v\n", row)
}
if err := rows.Close(); err != nil {
	fmt.Printf("N1QL query error: %s\n", err)
}

Because, actually ExecuteN1qlQuery takes two params:

func (b *Bucket) ExecuteN1qlQuery(q *N1qlQuery, params interface{}) (ViewResults, error)

I am not sure just how to use it... Like I would like to create a query with placeholders, and pass values to ExecuteN1qlQuery via params. Like with SQL (prepare -> execute). For example something like that:

    myQuery := gocb.NewN1qlQuery("SELECT * FROM default where a=? and b=?")
    rows, err := myBucket.ExecuteN1qlQuery(myQuery,[]string{"b","c"})
    if err != nil {
    	fmt.Printf("N1QL query error: %s\n", err)
    }
    var row interface{}
    for rows.Next(&row) {
    	fmt.Printf("Row: %+v\n", row)
    }
    if err := rows.Close(); err != nil {
    	fmt.Printf("N1QL query error: %s\n", err)
    }

答案1

得分: 4

你发布的示例是来自我们在 GitHub 上的开发者指南存储库:

https://github.com/couchbaselabs/devguide-examples/blob/master/go/query-placeholders.go.

基本上,你使用的是 $ 符号,它引用一个接口和一个以 1 开头的相应位置参数。

对于你的示例,它看起来像这样:

// 使用占位符设置新的查询
myQuery := gocb.NewN1qlQuery("SELECT * FROM default where a=$1 and b=$2")

// 为参数设置一个数组
var myParams []interface{}
myParams = append(myParams, "foo")
myParams = append(myParams, "bar")

// 执行查询
rows, err := bucket.ExecuteN1qlQuery(myQuery, myParams)
if err != nil {
fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
}

// 遍历行并打印输出
var row interface{}
for rows.Next(&row) {
fmt.Printf("Results: %+v\n", row)
}

英文:

The example you posted for how to do do this is from our developer guide repo on github:

https://github.com/couchbaselabs/devguide-examples/blob/master/go/query-placeholders.go.

Basically, you're using $ which references an interface and a corresponding positional parameter beginning with 1.

For your example it would look something like:

// Setup a new query with a placeholder
myQuery := gocb.NewN1qlQuery("SELECT * FROM default where a=$1 and b=$2")

// Setup an array for parameters
var myParams []interface{}
myParams = append(myParams,"foo")
myParams = append(myParams,"bar")

// Execute Query
rows, err := bucket.ExecuteN1qlQuery(myQuery, myParams)
if err != nil {
	fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
}

// Iterate through rows and print output
var row interface{}
for rows.Next(&row) {
	fmt.Printf("Results: %+v\n", row)
}

答案2

得分: 0

刚刚找到一个例子

        myQuery := gocb.NewN1qlQuery("SELECT airportname, city, country FROM `travel-sample` " +
        "WHERE type='airport' AND city=$1 ")

    // 为参数设置一个数组
    var myParams []interface{}
    myParams = append(myParams, "Reno")

    // 执行查询
    rows, err := bucket.ExecuteN1qlQuery(myQuery, myParams)
英文:

Just found example

        myQuery := gocb.NewN1qlQuery("SELECT airportname, city, country FROM `travel-sample` " +
		"WHERE type='airport' AND city=$1 ")

	// Setup an array for parameters
	var myParams []interface{}
	myParams = append(myParams, "Reno")

	// Execute Query
	rows, err := bucket.ExecuteN1qlQuery(myQuery, myParams)

huangapple
  • 本文由 发表于 2016年1月11日 13:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/34714956.html
匿名

发表评论

匿名网友

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

确定