stmt, err := db.Preparex("INSERT INTO users (name, email) VALUES (?, ?)") if err != nil { log.Fatal(err) } defer stmt.Close() result, err := stmt.Exec("John Doe", "[email protected]") if err != nil { log.Fatal(err) } rowsAffected, err := result.RowsAffected() if err != nil { log.Fatal(err) } log.Printf("%d rows affected", rowsAffected)
_, err := db.Exec("DELETE FROM users WHERE id = ?", 42) if err != nil { log.Fatal(err) }This example shows how to use `DB.Exec` to execute a SQL statement directly without preparing it first. The statement deletes a user with the ID of 42. In conclusion, the `github.com.jmoiron.sqlx` package library is a useful tool for database operations in Go, providing advanced features and convenience functions to simplify database interaction.