shad-go/lectures/07-sql/query/queryrow.go
2020-04-09 14:15:54 +03:00

24 lines
388 B
Go

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
}
log.Fatal(err)
}
log.Println(name)
}