stmt, err := db.Prepare("INSERT INTO users (name, age) VALUES (?, ?)") if err != nil { log.Fatal(err) } res, err := stmt.Exec("John Doe", 30) if err != nil { log.Fatal(err) } lastInsertId, err := res.LastInsertId() if err != nil { log.Fatal(err) } fmt.Printf("Last insert ID: %d", lastInsertId)
stmt, err := db.Prepare("UPDATE users SET age = ? WHERE name = ?") if err != nil { log.Fatal(err) } res, err := stmt.Exec(35, "John Doe") if err != nil { log.Fatal(err) } rowsAffected, err := res.RowsAffected() if err != nil { log.Fatal(err) } fmt.Printf("Rows affected: %d", rowsAffected)This example creates a prepared statement to update the age of the user with the name "John Doe" to 35. It then executes the statement and retrieves the number of rows affected, which can be useful when checking the success of an update operation. Both examples make use of the `Stmt` interface provided by the `database/sql` package.