我正在使用go-sqlite3来检索具有特定值的列的行数:query := "select count(notebook) from pages where notebook="result, err := db.Query(fmt.Sprint(query, id))Whereid传递给运行查询的函数。如何从中检索计数值result?
1 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
这应该有效:
// 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)
default:
fmt.Printf("Counted %s notebooks\n", output)
}
- 1 回答
- 0 关注
- 295 浏览
添加回答
举报
0/150
提交
取消