Android databases

1,370 views

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,370
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
52
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Android databases

  1. 1. Android Databases
  2. 2. Android Databases Comparación | Alternativas
  3. 3. SQLite
  4. 4. SQLite Sencillo Ligero Embebido
  5. 5. API Nativa
  6. 6. API Nativa SQLiteOpenHelper SQLiteDatabase Cursor
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. 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
  20. 20. 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
  21. 21. API Nativa
  22. 22. API Nativa Simple (no sencilla) Documentación Control y rendimiento Más código, más sucio Tendencia a errores Difícil de mantener
  23. 23. ORM Lite
  24. 24. ORM Lite Modelo Java OrmLiteSqliteOpenHelper DAO
  25. 25. 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
  26. 26. Definir modelo (Teléfono) @DatabaseTable
 public class PhoneNumber {
 
 @DatabaseField(generatedId = true)
 private int id;
 
 @DatabaseField
 private int number;
 }
 1
  27. 27. 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
  28. 28. 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
  29. 29. 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)
  30. 30. 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)
  31. 31. 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)
  32. 32. mDbHelper = new SampleSQLiteHelper(this); ! List<Contact> contacts = mDbHelper.getDaoContact().queryForAll(); 
 setListAdapter(new ContactAdapter(this, contacts)); 3 Usar DAOs (leer)
  33. 33. mDbHelper = new SampleSQLiteHelper(this); ! List<Contact> contacts = mDbHelper.getDaoContact().queryForAll(); 
 setListAdapter(new ContactAdapter(this, contacts)); 3 Usar DAOs (leer)
  34. 34. mDbHelper = new SampleSQLiteHelper(this); ! List<Contact> contacts = mDbHelper.getDaoContact().queryForAll(); 
 setListAdapter(new ContactAdapter(this, contacts)); 3 Usar DAOs (leer) *
  35. 35. 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
  36. 36. 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)
  37. 37. 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)
  38. 38. 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)
  39. 39. 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)
  40. 40. 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
  41. 41. ORM Lite
  42. 42. 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
  43. 43. Content Provider
  44. 44. Content Provider Tablas y columnas Almacenamiento independiente Entre aplicaciones
  45. 45. Ejemplo: Contacts Provider
  46. 46. Ejemplo: Contacts Provider
  47. 47. Ejemplo: Contacts Provider
  48. 48. Content Provider
  49. 49. Datos entre aplicaciones Permisos Integrado en Android Complejo Conocimientos avanzados Sólo una capa Content Provider
  50. 50. ¿Cuál elijo?
  51. 51. API Nativa Modelo impuesto Control total Rendimiento ORM Lite Objetos Java Separar capas Urgente Content Provider Información compartida Extensible Integrar
  52. 52. 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
  53. 53. +Rafa Vázquez ! sloydev.com " @sloydev

×