var name string // conn是sql.DB变量 // 做一个没有结果的SQL查询 err := conn.QueryRow("select name from user where id = 34").Scan(&name) if err == sql.ErrNoRows { log.Print("没有结果") } else { panic(err) }
// conn是sql.DB变量 // 做一个没有结果的SQL查询 rows, err := conn.Query("select name from user where id = 34") if err != nil { panic(err) } items := make([]string, 0, 0) defer rows.Close() rowLength := 0 for rows.Next() { rowLength++ var name string err = rows.Scan(&name) if err != nil { panic(err) } items = append(items, name) } err = rows.Err() if err != nil { panic(err) } if rowLength == 0 { log.Print("没有数据") }