douyi1939
2014-12-04 06:47
浏览 526

我们还应该在Golang中关闭DB的.Prepare()吗?

From this tutorial shown that rows.Closed() must be called where rows is from stmt.Query(), is stmt.Closed() also should be called where stmt is from db.Prepare()?

// inside a function
stmt, err := db.Prepare(cmd) // cmd is SQL string
Check(err)
// should we add: defer stmt.Close()
rows, err := stmt.Query(params) // params is map/interface{}
defer rows.Close()
Check(err)

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

来自教程显示必须在 rows 处调用 rows.Closed()。 code>来自 stmt.Query(),也是 stmt.Closed(),其中 stmt 来自 db的地方也应调用。 制备()

  //在函数内
stmt,err:= db.Prepare(cmd)// cmd是SQL字符串
Check(err)
 //应该添加: 延迟stmt.Close()
rows,err:= stmt.Query(params)//参数是map / interface {} 
defer rows.Close()
Check(err)
   \  n 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyi1921 2014-12-04 14:20
    已采纳

    The short answer is Yes. You should call stmt.Close();

    The long answer can be found in this google groups thread.

    已采纳该答案
    打赏 评论
  • dpge74512 2017-12-20 12:02

    Use as follows

    // inside a function
    stmt, err := db.Prepare(cmd) // cmd is SQL string
    if err != nil {
        println(err.Error())
    }
    defer stmt.Close()
    rows, err := stmt.Query(params) // params is map/interface{}
    
    if err != nil {
        println(err.Error())
    }
    
    打赏 评论

相关推荐 更多相似问题