使用golang的datadase/sql包进行mysql数据库查询,没有查询到结果时,返回值是多少
sqlStr := "select distinct domain from securitymanager.domainthreat where ip =?"
rows, err := db.Query(sqlStr, "222.73.219.63")
没有查询到数据时返回值:
- 返回的err为nil,没有查询到数据是正常的情况,不会报错
- rows不是nil!而是一个结构体的内存地址,比如
&{0xc042096080 0x4c89e0 0xc042078140 0x4ba8d0 0xc04203a1c0 {{0 0} 0 0 0 0} false <nil> []} - rows结构体格式
type Rows struct {
dc *driverConn // owned; must call releaseConn when closed to release
releaseConn func(error)
rowsi driver.Rows
cancel func() // called when Rows is closed, may be nil.
closeStmt *driverStmt // if non-nil, statement to Close on close
// closemu prevents Rows from closing while there
// is an active streaming result. It is held for read during non-close operations
// and exclusively during close.
//
// closemu guards lasterr and closed.
closemu sync.RWMutex
closed bool
lasterr error // non-nil only if closed is true
// lastcols is only used in Scan, Next, and NextResultSet which are expected
// not to be called concurrently.
lastcols []driver.Value
}
在使用Golang的database/sql包操作MySQL数据库时,如果查询无结果,会返回err为nil,表示没有错误。rows变量并不会返回nil,而是一个结构体的内存地址,表明查询操作已完成但未找到记录。
717

被折叠的 条评论
为什么被折叠?



