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

24 lines
399 B
Go

package prepare
import (
"context"
"database/sql"
"log"
)
func Prepare(ctx context.Context, db *sql.DB) {
stmt, err := db.PrepareContext(ctx, "SELECT name FROM users WHERE id = $1")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
for i := 1; ; i++ {
var name string
if err = stmt.QueryRowContext(ctx, i).Scan(&name); err != nil {
log.Fatal(err)
}
log.Println(name)
}
}