Last active
December 10, 2018 23:53
-
-
Save parzibyte/c4efb0bd423428abc48049d0ec3a0609 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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