shad-go/lectures/07-sql/query/queryrow.go

25 lines
388 B
Go
Raw Normal View History

2020-04-09 11:15:34 +00:00
package query
import (
"context"
"database/sql"
"errors"
"log"
)
func QueryRow(ctx context.Context, db *sql.DB) {
var id int
var name string
err := db.QueryRowContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1).Scan(&id, &name)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.Println("nothing found")
return
}
2020-04-08 19:20:23 +00:00
2020-04-09 11:15:34 +00:00
log.Fatal(err)
}
log.Println(name)
}