Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active December 10, 2018 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/c4efb0bd423428abc48049d0ec3a0609 to your computer and use it in GitHub Desktop.
Save parzibyte/c4efb0bd423428abc48049d0ec3a0609 to your computer and use it in GitHub Desktop.
func main() {
c := Contacto{
Nombre: "Luis Cabrera Benito",
Direccion: "Calle Sin Nombre #123",
CorreoElectronico: "contacto@parzibyte.me",
}
err := insertar(c)
if err != nil {
fmt.Printf("Error insertando: %v", err)
}else{
fmt.Println("Insertado correctamente")
}
}
func insertar(c Contacto) (e error) {
db, err := obtenerBaseDeDatos()
if err != nil {
return err
}
defer db.Close()
// Preparamos para prevenir inyecciones SQL
sentenciaPreparada, err := db.Prepare("INSERT INTO agenda (nombre, direccion, correo_electronico) VALUES(?, ?, ?)")
if err != nil {
return err
}
defer sentenciaPreparada.Close()
// Ejecutar sentencia, un valor por cada '?'
_, err = sentenciaPreparada.Exec(c.Nombre, c.Direccion, c.CorreoElectronico)
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment