Last active
July 1, 2020 16:12
-
-
Save rgdevment/15d4d31b5af8449ef158eb514650ef42 to your computer and use it in GitHub Desktop.
[Databse on ASndroid] Crear clase para utilizar datos localmente en Android con Java y SQLite #Android #Java #SQLite
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
package cl.atic.enlacesfcab.fcabenlaces; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteDatabase.CursorFactory; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class EnlacesSQLiteHelper extends SQLiteOpenHelper { | |
//Creamos la tabla | |
private String sqlCreate = "CREATE TABLE Enlaces (" + | |
"id NUMBER, " + | |
"lugar TEXT, " + | |
"nombre TEXT, " + | |
"libraje TEXT, " + | |
"angulo TEXT, " + | |
"tipo TEXT)"; | |
public EnlacesSQLiteHelper(Context contexto, String nombre, | |
CursorFactory factory, int version) { | |
super(contexto, nombre, factory, version); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
//Crea la tabla (SI no existe) | |
db.execSQL(sqlCreate); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) { | |
//En cas de una actualizacion, eliminamos la tabla y volvemos a llamar al metodo crear | |
//se puede eleborar algo mejor | |
db.execSQL("DROP TABLE IF EXISTS Enlaces"); | |
db.execSQL(sqlCreate); | |
} | |
} |
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
//Abrir base de datos para escribir. (La clase se encargara de crearla si no existe) | |
EnlacesSQLiteHelper usdbh = new EnlacesSQLiteHelper(this, "DB_enlaces", null, 1); | |
SQLiteDatabase db_write = usdbh.getWritableDatabase(); | |
if(db_write != null) | |
{ | |
for(int i=1; i<=10; i++) | |
{ | |
String lugar = "Enlace " + i; | |
db_write.execSQL("INSERT INTO Enlaces (id, lugar) " + | |
"VALUES (" + i + ", '" + lugar +"')"); | |
} | |
//Cerrar cuando terminamos | |
db_write.close(); | |
} |
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
//Abrir la db en modo lectura | |
SQLiteDatabase db_reader = usdbh.getReadableDatabase(); | |
if(db_reader != null) | |
{ | |
Cursor c = db_reader.rawQuery("SELECT id, lugar FROM Enlaces", null); | |
if (c.moveToFirst()){ | |
do { | |
//almacenar los resultados en propiedades para una ListView | |
PropertyClass.setLugarEnlace(c.getString(0)); | |
PropertyClass.setNombreEnlace(c.getString(1)); | |
PropertyClass.setRielEnlace(c.getString(1)); | |
PropertyClass.setAnguloEnlace(c.getString(1)); | |
PropertyClass.setTipoEnlace(c.getString(1)); | |
} while(c.moveToNext()); | |
} | |
c.close(); | |
db_reader.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment