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

20 lines
353 B
Go
Raw 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) {
res, err := db.ExecContext(ctx, "UPDATE users SET name = $1 WHERE id = $2", "William Mandella", 1)
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)
}