Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Object Relational model for SQLIite in android

210 visualizaciones

Publicado el

Orm is use ful for getting the database records in the object model

Publicado en: Educación
  • Sé el primero en comentar

  • Sé el primero en recomendar esto

Object Relational model for SQLIite in android

  1. 1. ORM
  2. 2. ORM Object-relational mapping products integrate objectObject-relational mapping products integrate object programming language capabilities with relationalprogramming language capabilities with relational databasesdatabases
  3. 3. What is ORMLiteWhat is ORMLite • Object Relational Mapping Lite (ORM Lite)Object Relational Mapping Lite (ORM Lite) provides some lightweight functionality forprovides some lightweight functionality for persisting Java objects to SQL databasespersisting Java objects to SQL databases while avoiding the complexity and overheadwhile avoiding the complexity and overhead of more standard ORM packages.of more standard ORM packages.
  4. 4. Features of ORMLiteFeatures of ORMLite • Setup your classes by simply adding JavaSetup your classes by simply adding Java annotations.annotations. • EgEg @DatabaseTable(tableName =@DatabaseTable(tableName = "Contato""Contato")) • Powerful abstract Database Access ObjectPowerful abstract Database Access Object (DAO) classes(DAO) classes • Flexible QueryBuilder to easily constructFlexible QueryBuilder to easily construct simple and complex queries.simple and complex queries. • Basic support for database transactions.Basic support for database transactions. • Auto generates SQL to create and dropAuto generates SQL to create and drop database tables.database tables.
  5. 5. Simple ExampleSimple Example • @DatabaseTable(tableName =@DatabaseTable(tableName = "Contato""Contato")//table name)//table name public classpublic class ContatoEntity {ContatoEntity { @DatabaseField(generatedId =@DatabaseField(generatedId = truetrue))public int idpublic int id; //field name; //field name @DatabaseField@DatabaseField public staticpublic static StringString discricodiscrico;; @DatabaseField@DatabaseField publicpublic StringString ddiddi;; @DatabaseField@DatabaseField publicpublic StringString dddddd;; @DatabaseField@DatabaseField publicpublic StringString timeStamptimeStamp;; publicpublic ContatoEntity() {ContatoEntity() { }} }} • table name Contatotable name Contato id discrico ddi ddd time Stamp
  6. 6. ORMLiteHelperClassORMLiteHelperClass • public class ORMLiteSqliteOpenHelper extends OrmLiteSqliteOpenHelper {public class ORMLiteSqliteOpenHelper extends OrmLiteSqliteOpenHelper { • public ORMLiteSqliteOpenHelper(Context context) {public ORMLiteSqliteOpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION);super(context, DATABASE_NAME, null, DATABASE_VERSION); }} @Override@Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSourcepublic void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {connectionSource) { TableUtils.createTable(connectionSource, ContatoEntity.class);TableUtils.createTable(connectionSource, ContatoEntity.class); }} @Override@Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSourcepublic void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {connectionSource, int i, int i1) { TableUtils.dropTable(connectionSource,ContatoEntity.class,true);TableUtils.dropTable(connectionSource,ContatoEntity.class,true); }} @Override@Override public void close() {public void close() { super.close();super.close(); }} }}
  7. 7. DAO CreationDAO Creation • publicpublic Dao<ContatoEntity, Integer>Dao<ContatoEntity, Integer> contatoEntitiesDaocontatoEntitiesDao;; • contatoEntitiesDaocontatoEntitiesDao==ormLiteSqliteOpenHelperormLiteSqliteOpenHelper.getDao(.getDao(ContatoEntity.ContatoEntity.classclass);); Table ClassTable Class NameName we can give this two line where ever we needwe can give this two line where ever we need in our activitys or any other classin our activitys or any other class
  8. 8. Insertion into the tableInsertion into the table • ContatoEntity contatoEntity=ContatoEntity contatoEntity=newnew ContatoEntity();ContatoEntity(); contatoEntity.contatoEntity.dddddd=="one""one";; contatoEntity.contatoEntity.ddiddi=="two""two";; contatoEntity.contatoEntity.discricodiscrico=="threee""threee";; contatoEntity.contatoEntity.timeStamptimeStamp=String.=String.valueOfvalueOf(System.(System.currentTimeMilliscurrentTimeMillis());()); contatoEntitiesDao.contatoEntitiesDao.createcreate(contatoEntity(contatoEntity););//row is inserted into the table//row is inserted into the table
  9. 9. Retrive from the Database in the form javaRetrive from the Database in the form java objectsobjects • List<ContatoEntity>List<ContatoEntity> demo=queryBuilder.selectColumns(demo=queryBuilder.selectColumns("discrico""discrico").query();).query(); • forfor((intint rowCount=0;rowCount<demo.size();rowCount++)rowCount=0;rowCount<demo.size();rowCount++) {{ ContatoEntity contatoEntity1=demo.get(rowCount);//gettingContatoEntity contatoEntity1=demo.get(rowCount);//getting in the form java object of the TableTypein the form java object of the TableType Log.Log.ee(MainActivity.(MainActivity.classclass.getName(),.getName(), "discrico:"discrico: ""+contatoEntity1.+contatoEntity1.discricodiscrico );); }}
  10. 10. AdvantagesAdvantages • Use for complicated databaseUse for complicated database operationsoperations • No need to remember to SQL queriesNo need to remember to SQL queries • Prefer for big size applicationPrefer for big size application
  11. 11. •Use for complicated database operations •No need to remember to SQL queries •Prefer for big size application •Poor performance and scalability compared to other data storage methods. •Unnecessarily increase size of application •Little bit slow with compare to greenDao(another ORM) AdvantagesAdvantages DisadvantagesDisadvantages

×