Created
September 28, 2017 04:07
-
-
Save luisrenemas/01b138446f8fad3418be1f4ebcc1ce75 to your computer and use it in GitHub Desktop.
PaisesDataBaseHelper - Para usar en un Spinner widget Android
This file contains hidden or 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
//Cambia esta línea de código | |
package com.a01luisrene.tutoriales.modelos; | |
public class Pais { | |
private String id; | |
private String nombrePais; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getNombrePais() { | |
return nombrePais; | |
} | |
public void setNombrePais(String nombrePais) { | |
this.nombrePais = nombrePais; | |
} | |
public Pais(String id, String nombrePais) { | |
this.id = id; | |
this.nombrePais = nombrePais; | |
} | |
} |
This file contains hidden or 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
//Cambia esta línea de código | |
package com.a01luisrene.tutoriales; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import android.util.Log; | |
import com.a01luisrene.tutoriales.modelos.Pais; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class PaisesDataBaseHelper extends SQLiteOpenHelper{ | |
private static final String TAG = "logcat"; | |
private static PaisesDataBaseHelper sInstance; | |
private static final String NOMBRE_BASE_DATOS = "paises.db"; | |
private static final int DATABASE_VERSION = 1; | |
//Nombre de la tabla | |
private static final String TABLA_PAISES = "paises"; | |
//Nombre de las columnas de la tabla paises | |
private static final String ID_PAIS = "_id"; | |
private static final String NOMBRE_PAIS = "nombre_pais"; | |
//Patrón singleton - info: http://bit.ly/6LRzfx | |
public static synchronized PaisesDataBaseHelper getInstance(Context context) { | |
// Utilice el contexto de la aplicación, que garantizará que | |
// no se filtra accidentalmente el contexto de una actividad. | |
if (sInstance == null) { | |
sInstance = new PaisesDataBaseHelper(context.getApplicationContext()); | |
} | |
return sInstance; | |
} | |
private PaisesDataBaseHelper(Context context) { | |
super(context, NOMBRE_BASE_DATOS, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onConfigure(SQLiteDatabase db) { | |
super.onConfigure(db); | |
db.setForeignKeyConstraintsEnabled(true); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
String CREAR_TABLA_PAIS = "CREATE TABLE " + TABLA_PAISES + | |
"(" + | |
ID_PAIS + " INTEGER PRIMARY KEY AUTOINCREMENT," + | |
NOMBRE_PAIS + " TEXT NOT NULL "+ | |
")"; | |
db.execSQL(CREAR_TABLA_PAIS); | |
//Inserto en la base de datos, los nombres de países por única vez | |
insertarPaises(db); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
if (oldVersion != newVersion) { | |
String tablaPaisesV2 = "DROP TABLE IF EXISTS " + TABLA_PAISES; | |
db.execSQL(tablaPaisesV2); | |
onCreate(db); | |
} | |
} | |
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
onUpgrade(db, oldVersion, newVersion); | |
} | |
private void insertarPaises(SQLiteDatabase db) { | |
insertarDatos(db, valores(null, "Perú")); | |
insertarDatos(db, valores(null, "Ecuador")); | |
insertarDatos(db, valores(null, "Chile")); | |
insertarDatos(db, valores(null, "Colombia")); | |
insertarDatos(db, valores(null, "Brazil")); | |
} | |
private long insertarDatos(SQLiteDatabase db, ContentValues valores) { | |
return db.insert( | |
PaisesDataBaseHelper.TABLA_PAISES, | |
null, | |
valores); | |
} | |
private ContentValues valores(String id, String tituloPais){ | |
// Contenedor de valores | |
ContentValues values = new ContentValues(); | |
// Pares clave-valor | |
values.put(PaisesDataBaseHelper.ID_PAIS, id); | |
values.put(PaisesDataBaseHelper.NOMBRE_PAIS, tituloPais); | |
return values; | |
} | |
public List<Pais> getAllPaises() { | |
List<Pais> paises = new ArrayList<>(); | |
String QUERY_SELECCIONAR_PAIS = | |
String.format("SELECT %s, %s FROM %s", | |
ID_PAIS, | |
NOMBRE_PAIS, | |
TABLA_PAISES); | |
SQLiteDatabase db = getReadableDatabase(); | |
Cursor cursor = db.rawQuery(QUERY_SELECCIONAR_PAIS, null); | |
try { | |
if (cursor.moveToFirst()) { | |
do { | |
Pais nuevoPais = new Pais( | |
cursor.getString(0), | |
cursor.getString(1) | |
); | |
paises.add(nuevoPais); | |
} while(cursor.moveToNext()); | |
} | |
} catch (Exception e) { | |
Log.d(TAG, "Error mientras se intenta conseguir los registros de la base de datos"); | |
} finally { | |
if (cursor != null && !cursor.isClosed()) { | |
cursor.close(); | |
} | |
} | |
return paises; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment