Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Android Databases (Español)
Search
Sloy
May 26, 2014
Technology
1
60
Android Databases (Español)
Android Databases, alternativas de uso
Sloy
May 26, 2014
Tweet
Share
Other Decks in Technology
See All in Technology
Claude Code に プロジェクト管理やらせたみた
unson
6
3.9k
Delegating the chores of authenticating users to Keycloak
ahus1
0
140
KubeCon + CloudNativeCon Japan 2025 Recap by CA
ponkio_o
PRO
0
300
ビズリーチが挑む メトリクスを活用した技術的負債の解消 / dev-productivity-con2025
visional_engineering_and_design
3
7.5k
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
27k
Oracle Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
0
110
OPENLOGI Company Profile
hr01
0
67k
CDKTFについてざっくり理解する!!~CloudFormationからCDKTFへ変換するツールも作ってみた~
masakiokuda
1
130
生成AI開発案件におけるClineの業務活用事例とTips
shinya337
0
250
Operating Operator
shhnjk
1
580
Tokyo_reInforce_2025_recap_iam_access_analyzer
hiashisan
0
180
MUITにおける開発プロセスモダナイズの取り組みと開発生産性可視化の取り組みについて / Modernize the Development Process and Visualize Development Productivity at MUIT
muit
1
16k
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Code Reviewing Like a Champion
maltzj
524
40k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
For a Future-Friendly Web
brad_frost
179
9.8k
The Invisible Side of Design
smashingmag
301
51k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Building an army of robots
kneath
306
45k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.4k
BBQ
matthewcrist
89
9.7k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
950
Transcript
Android Databases +Rafael Vázquez
Android Databases Comparación | Alternativas
SQLite
SQLite Sencillo Ligero Embebido
None
API Nativa
API Nativa SQLiteOpenHelper SQLiteDatabase Cursor
Extender SQLiteOpenHelper public static final String DATABASE_NAME = "contacts.db"; public
static final int DATABASE_VERSION = 1; public static final String TABLE_CONTACTS = "contact"; public static final String FIELD_ID = "_id"; public static final String FIELD_NAME = "name"; public static final String FIELD_NUMBER = "number"; public SampleSQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "( " + FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FIELD_NAME + " TEXT, " + FIELD_NUMBER + " NUMBER);"); } 1
Extender SQLiteOpenHelper public static final String DATABASE_NAME = "contacts.db"; public
static final int DATABASE_VERSION = 1; public static final String TABLE_CONTACTS = "contact"; public static final String FIELD_ID = "_id"; public static final String FIELD_NAME = "name"; public static final String FIELD_NUMBER = "number"; public SampleSQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "( " + FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FIELD_NAME + " TEXT, " + FIELD_NUMBER + " NUMBER);"); } 1
Extender SQLiteOpenHelper public static final String DATABASE_NAME = "contacts.db"; public
static final int DATABASE_VERSION = 1; public static final String TABLE_CONTACTS = "contact"; public static final String FIELD_ID = "_id"; public static final String FIELD_NAME = "name"; public static final String FIELD_NUMBER = "number"; public SampleSQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "( " + FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FIELD_NAME + " TEXT, " + FIELD_NUMBER + " NUMBER);"); } 1
Extender SQLiteOpenHelper public static final String DATABASE_NAME = "contacts.db"; public
static final int DATABASE_VERSION = 1; public static final String TABLE_CONTACTS = "contact"; public static final String FIELD_ID = "_id"; public static final String FIELD_NAME = "name"; public static final String FIELD_NUMBER = "number"; public SampleSQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "( " + FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FIELD_NAME + " TEXT, " + FIELD_NUMBER + " NUMBER);"); } 1
Extender SQLiteOpenHelper public static final String DATABASE_NAME = "contacts.db"; public
static final int DATABASE_VERSION = 1; public static final String TABLE_CONTACTS = "contact"; public static final String FIELD_ID = "_id"; public static final String FIELD_NAME = "name"; public static final String FIELD_NUMBER = "number"; public SampleSQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "( " + FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FIELD_NAME + " TEXT, " + FIELD_NUMBER + " NUMBER);"); } 1
Usar SQLiteDatabase (leer) mDbHelper = new SampleSQLiteHelper(this); ! SQLiteDatabase db
= mDbHelper.getReadableDatabase(); String[] projection = {FIELD_ID, FIELD_NAME, FIELD_NUMBER}; String sortOrder = FIELD_NAME; Cursor cursor = db.query(TABLE_CONTACTS, projection, null, null, null, null, sortOrder); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_2, cursor, new String[]{FIELD_NAME, FIELD_NUMBER}, new int[]{android.R.id.text1, android.R.id.text2} ); setListAdapter(adapter); db.close(); 2
Usar SQLiteDatabase (leer) mDbHelper = new SampleSQLiteHelper(this); ! SQLiteDatabase db
= mDbHelper.getReadableDatabase(); String[] projection = {FIELD_ID, FIELD_NAME, FIELD_NUMBER}; String sortOrder = FIELD_NAME; Cursor cursor = db.query(TABLE_CONTACTS, projection, null, null, null, null, sortOrder); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_2, cursor, new String[]{FIELD_NAME, FIELD_NUMBER}, new int[]{android.R.id.text1, android.R.id.text2} ); setListAdapter(adapter); db.close(); 2
Usar SQLiteDatabase (leer) mDbHelper = new SampleSQLiteHelper(this); ! SQLiteDatabase db
= mDbHelper.getReadableDatabase(); String[] projection = {FIELD_ID, FIELD_NAME, FIELD_NUMBER}; String sortOrder = FIELD_NAME; Cursor cursor = db.query(TABLE_CONTACTS, projection, null, null, null, null, sortOrder); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_2, cursor, new String[]{FIELD_NAME, FIELD_NUMBER}, new int[]{android.R.id.text1, android.R.id.text2} ); setListAdapter(adapter); db.close(); 2
Usar SQLiteDatabase (leer) mDbHelper = new SampleSQLiteHelper(this); ! SQLiteDatabase db
= mDbHelper.getReadableDatabase(); String[] projection = {FIELD_ID, FIELD_NAME, FIELD_NUMBER}; String sortOrder = FIELD_NAME; Cursor cursor = db.query(TABLE_CONTACTS, projection, null, null, null, null, sortOrder); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_2, cursor, new String[]{FIELD_NAME, FIELD_NUMBER}, new int[]{android.R.id.text1, android.R.id.text2} ); setListAdapter(adapter); db.close(); 2
Usar SQLiteDatabase (leer) mDbHelper = new SampleSQLiteHelper(this); ! SQLiteDatabase db
= mDbHelper.getReadableDatabase(); String[] projection = {FIELD_ID, FIELD_NAME, FIELD_NUMBER}; String sortOrder = FIELD_NAME; Cursor cursor = db.query(TABLE_CONTACTS, projection, null, null, null, null, sortOrder); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_2, cursor, new String[]{FIELD_NAME, FIELD_NUMBER}, new int[]{android.R.id.text1, android.R.id.text2} ); setListAdapter(adapter); db.close(); 2
Usar SQLiteDatabase (escribir) SQLiteDatabase db = mDbHelper.getWritableDatabase(); ContentValues values
= new ContentValues(2); values.put(FIELD_NAME, contactName); values.put(FIELD_NUMBER, contactNumber); db.insert(TABLE_CONTACTS, null, values); db.close(); 3
Usar SQLiteDatabase (escribir) SQLiteDatabase db = mDbHelper.getWritableDatabase(); ContentValues values
= new ContentValues(2); values.put(FIELD_NAME, contactName); values.put(FIELD_NUMBER, contactNumber); db.insert(TABLE_CONTACTS, null, values); db.close(); 3
Usar SQLiteDatabase (escribir) SQLiteDatabase db = mDbHelper.getWritableDatabase(); ContentValues values
= new ContentValues(2); values.put(FIELD_NAME, contactName); values.put(FIELD_NUMBER, contactNumber); db.insert(TABLE_CONTACTS, null, values); db.close(); 3
Usar SQLiteDatabase (escribir) SQLiteDatabase db = mDbHelper.getWritableDatabase(); ContentValues values
= new ContentValues(2); values.put(FIELD_NAME, contactName); values.put(FIELD_NUMBER, contactNumber); db.insert(TABLE_CONTACTS, null, values); db.close(); 3
API Nativa
API Nativa Simple (no sencilla) Documentación Control y rendimiento Más
código, más sucio Tendencia a errores Difícil de mantener
None
ORM Lite
ORM Lite Modelo Java OrmLiteSqliteOpenHelper DAO
Definir modelo (Contacto) @DatabaseTable public class Contact { @DatabaseField(generatedId
= true) private int id; @DatabaseField private String name; @DatabaseField(foreign = true, foreignAutoRefresh = true, foreignAutoCreate = true) private PhoneNumber phoneNumber; } 1
Definir modelo (Teléfono) @DatabaseTable public class PhoneNumber { @DatabaseField(generatedId
= true) private int id; @DatabaseField private int number; } 1
Extender OrmLiteSqliteOpenHelper private static final int DB_VERSION = 1; private
static final String DB_NAME = "contacts.db"; public DbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db, ConnectionSource cs) { try { TableUtils.createTable(cs, Contact.class); TableUtils.createTable(cs, PhoneNumber.class); } catch (SQLException e) { throw new RuntimeException(e); } } 2
Extender OrmLiteSqliteOpenHelper private static final int DB_VERSION = 1; private
static final String DB_NAME = "contacts.db"; public DbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db, ConnectionSource cs) { try { TableUtils.createTable(cs, Contact.class); TableUtils.createTable(cs, PhoneNumber.class); } catch (SQLException e) { throw new RuntimeException(e); } } 2
private RuntimeExceptionDao<Contact, Integer> contactDao; private RuntimeExceptionDao<PhoneNumber, Integer> phoneDao; ! !
! public RuntimeExceptionDao<Contact, Integer> getDaoContact() { if (contactDao == null) { contactDao = getRuntimeExceptionDao(Contact.class); } return contactDao; } public RuntimeExceptionDao<PhoneNumber, Integer> getDaoPhone() { if (phoneDao == null) { phoneDao = getRuntimeExceptionDao(PhoneNumber.class); } return phoneDao; } 2 Extender OrmLiteSqliteOpenHelper (DAO)
private RuntimeExceptionDao<Contact, Integer> contactDao; private RuntimeExceptionDao<PhoneNumber, Integer> phoneDao; ! !
! public RuntimeExceptionDao<Contact, Integer> getDaoContact() { if (contactDao == null) { contactDao = getRuntimeExceptionDao(Contact.class); } return contactDao; } public RuntimeExceptionDao<PhoneNumber, Integer> getDaoPhone() { if (phoneDao == null) { phoneDao = getRuntimeExceptionDao(PhoneNumber.class); } return phoneDao; } 2 Extender OrmLiteSqliteOpenHelper (DAO)
private RuntimeExceptionDao<Contact, Integer> contactDao; private RuntimeExceptionDao<PhoneNumber, Integer> phoneDao; ! !
! public RuntimeExceptionDao<Contact, Integer> getDaoContact() { if (contactDao == null) { contactDao = getRuntimeExceptionDao(Contact.class); } return contactDao; } public RuntimeExceptionDao<PhoneNumber, Integer> getDaoPhone() { if (phoneDao == null) { phoneDao = getRuntimeExceptionDao(PhoneNumber.class); } return phoneDao; } 2 Extender OrmLiteSqliteOpenHelper (DAO)
mDbHelper = new SampleSQLiteHelper(this); ! List<Contact> contacts = mDbHelper.getDaoContact().queryForAll();
setListAdapter(new ContactAdapter(this, contacts)); 3 Usar DAOs (leer)
mDbHelper = new SampleSQLiteHelper(this); ! List<Contact> contacts = mDbHelper.getDaoContact().queryForAll();
setListAdapter(new ContactAdapter(this, contacts)); 3 Usar DAOs (leer)
mDbHelper = new SampleSQLiteHelper(this); ! List<Contact> contacts = mDbHelper.getDaoContact().queryForAll();
setListAdapter(new ContactAdapter(this, contacts)); 3 Usar DAOs (leer) *
public class ContactAdapter extends BaseAdapter { // ...
@Override public View getView(int position, View v, ViewGroup p) { v = inflater.inflate(android.R.layout.simple_list_item_2, p, false); Contact contact = getItem(position); TextView text1 = (TextView) v.findViewById(android.R.id.text1); TextView text2 = (TextView) v.findViewById(android.R.id.text2); text1.setText(contact.getName()); text2.setText(contact.getPhoneNumber().getNumber().toString()); return v; } } * Extra: BaseAdapter
Contact contact = new Contact(); contact.setName(name); PhoneNumber phoneNumber =
new PhoneNumber(); phoneNumber.setNumber(number); contact.setPhoneNumber(phoneNumber); mDbHelper.getDaoContact().create(contact); 3 Usar DAOs (escribir)
Contact contact = new Contact(); contact.setName(name); PhoneNumber phoneNumber =
new PhoneNumber(); phoneNumber.setNumber(number); contact.setPhoneNumber(phoneNumber); mDbHelper.getDaoContact().create(contact); 3 Usar DAOs (escribir)
Contact contact = new Contact(); contact.setName(name); PhoneNumber phoneNumber =
new PhoneNumber(); phoneNumber.setNumber(number); contact.setPhoneNumber(phoneNumber); mDbHelper.getDaoContact().create(contact); 3 Usar DAOs (escribir)
Contact contact = new Contact(); contact.setName(name); PhoneNumber phoneNumber =
new PhoneNumber(); phoneNumber.setNumber(number); contact.setPhoneNumber(phoneNumber); mDbHelper.getDaoContact().create(contact); 3 Usar DAOs (escribir)
Contact contact = new Contact(); contact.setName(name); PhoneNumber phoneNumber =
new PhoneNumber(); phoneNumber.setNumber(number); contact.setPhoneNumber(phoneNumber); mDbHelper.getDaoContact().create(contact); 3 Usar DAOs (escribir) foreignAutoCreate = true
ORM Lite
ORM Lite Fácil de usar Generación automática Transformación tabla-objeto Modelos
Java Más allá de Android No centrado en Android No reaprovecha componentes
None
Content Provider
Content Provider Tablas y columnas Almacenamiento independiente Entre aplicaciones
Ejemplo: Contacts Provider
Ejemplo: Contacts Provider
None
Ejemplo: Contacts Provider
Content Provider
Datos entre aplicaciones Permisos Integrado en Android Complejo Conocimientos avanzados
Sólo una capa Content Provider
None
¿Cuál elijo?
API Nativa Modelo impuesto Control total Rendimiento ORM Lite Objetos
Java Separar capas Urgente Content Provider Información compartida Extensible Integrar
Otras opciones ADA Framework adaframework.com Android Dataframework github.com/javipacheco/Android-DataFramework greenDAO greendao-orm.com/
Sugar ORM satyan.github.io/sugar/ Retrofit (REST) github.com/square/retrofit
+Rafa Vázquez ! sloydev.com " @sloydev