Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 20, 2022 18:35
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/3634c07c9d564274bd907fc19d4432f0 to your computer and use it in GitHub Desktop.
Save parzibyte/3634c07c9d564274bd907fc19d4432f0 to your computer and use it in GitHub Desktop.
func existeColumnaEnTabla(tabla string, columna string) (bool, error) {
bd, err := obtenerBaseDeDatos()
if err != nil {
return false, err
}
defer bd.Close()
filas, err := bd.Query(`PRAGMA table_info("` + tabla + `")`)
/*
Lo de PRAGMA regresa algo así:
cid|name|type|notnull|dflt_value|pk
"0" "id" "integer" "0" "1"
"1" "id_grupo" "integer" "0" "0"
"2" "nombre" "text" "1" "0"
"3" "direccion" "text" "1" "0"
"4" "genero" "text" "1" "0"
"5" "edad" "integer" "1" "0"
"6" "nombre_tutor" "text" "1" "0"
"7" "numero_tutor" "text" "1" "0"
*/
if err != nil {
return false, err
}
defer filas.Close()
var cid string
var name string
var _type string // Con _ porque "type" es reservada
var notnull string
var dflt_value *string // Apuntador porque puede ser nulo, igual no no importa
var pk string
for filas.Next() {
err := filas.Scan(&cid, &name, &_type, &notnull, &dflt_value, &pk)
if err != nil {
return false, err
}
if name == columna {
return true, nil
}
}
return false, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment