简体   繁体   中英

Handling a “complex” database in Android SQLite

1 - Extending many times the SQLiteOpenHelper is wrong and wont work. 2 - Having a big class that extends SQLiteOpenHelper to handle (CRUD) 7 different classes is strange.

Is there an example to do this? CRUD many entities into my database without an enourmous and dependent class?

I can't seem to find examples bigger than 2 or 3 tables on Google. Having a very big SQLiteOpenHelper with save methods for all of my entities is horrible. ORM is not allowed on this project either.

Any help/directions?

Having a very big SQLiteOpenHelper with save methods for all of my entities is horrible.

Do you think that good designated application will have only a few code lines?

In Android extending from SQLiteOpenHelper is the best choice as possible. It wraps all required logic and work with it is in my opinion very comfortable. But all requires correct implementation.

Extending many times the SQLiteOpenHelper is wrong and wont work

Most likely there is not a problem with SQLiteOpenHleper but wrong implementation and usage i think.

How you meant, ORM frameworks for example as ORMLite is not allowed so my suggestion is to use SQLiteOpenHelper and make correct and clean implementation.

Update:

7 save methods. 7 update methods. 7 delete mehotds. 7 select methods. MANY other methods to get other data from the database. Is it the best choice?

How i meant in comments this is problem of implementation. You really don't need 7 save methods above 7 tables. Here is my first idea. What about to create one universal method?

public class DataSourceTools {

   private SQLiteOpenHelper handler;
   private SQLiteDatabase db;

   public DataSourceTools(SQLiteOpenHelper handler) {
      this.handler = handler;
   }

   public void saveObject(String table, ContentValues data) {
      try {
         db = openWrite(this.handler);
         if (db != null) {
            db.insert(table, nullColumnHackName, data);
         }
      }
      finally {
         close(db);
      }
   }

   public void updateObject(String table, ContentValues dataToUpdate) {
      try {
         db = openWrite(this.handler);
         if (db != null) {
            String whereClause = "...";
            String[] whereArgs = {...};
            db.update(table, dataToUpdate, whereClause, whereArgs);
         }
      }
      finally {
         close(db);
      }
   }

   public void deleteObject(String table, ContentValues data) {
      try {
         db = openWrite(this.handler);
         if (db != null) {
            String whereClause = "...";
            String[] whereArgs = {...};
            db.delete(table, whereClause, whereArgs);
         }
      }
      finally {
         close(db);
      }
   }

   public Object findObject(String table, ContentValues data) {
      Object myObject = null; 
      Cursor c = null;
      try {
         String[] columns = {"id", "name", "lastname", ...};
         String selection = "id = ?";
         String[] selectionArgs = {data.getAsString("key_id")};
         c = db.query(table, columns, selection, selectionArgs, null, null, null);
         if (c.moveToFirst()) {
            myObject = new Object();
            myObject.setId(c.getInt(c.getColumnIndex("id")));
            myObject.setName(c.getString(c.getColumnIndex("name")));
            myObject.setLastName(c.getString(c.getColumnIndex("lastname")));
         }
         return myObject;
      }
      finally {
         if (c != null) {
            c.close()
         }
         close(db);
      }
   }

   public List<Object> findAll(String table) {
      List<Object> objects = new ArrayList<Object>(); 
      Object myObject = null; 
      Cursor c = null;
      try {
         String[] columns = {"id", "name", "lastname", ...};
         c = db.query(table, columns, null, null, null, null, null);
         if (c.moveToFirst()) {
            myObject = new Object();
            myObject.setId(c.getInt(c.getColumnIndex("id")));
            myObject.setName(c.getString(c.getColumnIndex("name")));
            myObject.setLastName(c.getString(c.getColumnIndex("lastname")));
            objects.add(myObject);
         }
         return objects;
      }
      finally {
         if (c != null) {
            c.close()
         }
         close(db);
      }
   }

   private final synchronized SQLiteDatabase openWrite(SQLiteOpenHelper handler) {
      if (handler != null) {
         return handler.getWritableDatabase();
      }
      return null;
   }

   private final synchronized SQLiteDatabase openRead(SQLiteOpenHelper handler) {
      if (handler != null) {
         return handler.getReadableDatabase();
      }
      return null;
   }

   private final synchronized void close(SQLiteDatabase db) {
      if (db != null && db.isOpen()) {
         db.close();
      }
   }
}

Note: This is first concept, just now written especially for you so it needs updates: think about parameters of methods --> how to make better choice, usage of transactions, datasource as singleton, make some performance test for inserting, updating. All depend on character of application.

Using parameterized methods is preferable to using one method for each operation on each table. MBarni, can you provide an example of why this is not flexible enough for your needs?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM