2015-12-12 03:49
浏览 307


I am using go-sqlite3 to retrieve the number of rows with a column of a certain value:

query := "select count(notebook) from pages where notebook="
result, err := db.Query(fmt.Sprint(query, id))

Where id is passed to the function running the query.

How can I retrieve the count value from result?

图片转代码服务由CSDN问答提供 功能建议

我正在使用 go-sqlite3 检索具有特定值的列的行数:

  query:=“从页面中选择计数(笔记本) 其中notebook =“ 
result,err:= db.Query(fmt.Sprint(query,id))

其中 id 是 传递给运行查询的函数。


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsu5188 2015-12-12 04:27

    This should work:

    // Output will be stored here.
    var output string
    id := "1234"
     // Prepare your query
    query, err := db.Prepare("select count(notebook) from pages where notebook = ?")
    if err != nil {
        fmt.Printf("%s", err)
    defer query.Close()
    // Execute query using 'id' and place value into 'output'
    err = query.QueryRow(id).Scan(&output)
    // Catch errors
    switch {
    case err == sql.ErrNoRows:
            fmt.Printf("No notebook with that ID.")
    case err != nil:
            fmt.Printf("%s", err)
            fmt.Printf("Counted %s notebooks
    ", output)
    点赞 打赏 评论

相关推荐 更多相似问题