shad-go/lectures/07-sql/exec/exec.go

24 lines
363 B
Go
Raw Permalink Normal View History

2020-04-09 11:15:34 +00:00
package exec
import (
"context"
"database/sql"
"log"
)
2020-04-08 19:20:23 +00:00
2020-04-09 11:15:34 +00:00
func Exec(ctx context.Context, db *sql.DB) {
2021-04-08 11:37:21 +00:00
res, err := db.ExecContext(
ctx,
"UPDATE users SET name = $1 WHERE id = $2",
"William Mandella", 1,
)
2020-04-09 11:15:34 +00:00
if err != nil {
log.Fatal(err)
}
2020-04-08 19:20:23 +00:00
2020-04-09 11:15:34 +00:00
lastID, _ := res.LastInsertId()
rowsAffected, _ := res.RowsAffected()
log.Println(lastID, rowsAffected)
}