Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Databases (Español)

Sloy
May 26, 2014

Android Databases (Español)

Android Databases, alternativas de uso

Sloy

May 26, 2014
Tweet

Other Decks in Technology

Transcript

  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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. API Nativa Simple (no sencilla) Documentación Control y rendimiento Más

    código, más sucio Tendencia a errores Difícil de mantener
  16. 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
  17. Definir modelo (Teléfono) @DatabaseTable
 public class PhoneNumber {
 
 @DatabaseField(generatedId

    = true)
 private int id;
 
 @DatabaseField
 private int number;
 }
 1
  18. 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
  19. 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
  20. 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)
  21. 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)
  22. 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)
  23. 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
  24. 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)
  25. 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)
  26. 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)
  27. 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)
  28. 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
  29. 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
  30. API Nativa Modelo impuesto Control total Rendimiento ORM Lite Objetos

    Java Separar capas Urgente Content Provider Información compartida Extensible Integrar