更有效的方法在Android数据库中存储信息

时间:2015-01-30 04:27:39

标签: android json

现在,我一直在使用JSON格式在Android应用程序中存储信息。

然而,这有时是凌乱的,我觉得它在某些方面效率低下。我只是有一个更一般的问题:有没有更有效的方法在Android应用程序中存储信息?

1 个答案:

答案 0 :(得分:1)

您可以通过以下四种方式store information in Android

  1. 坚持使用自定义数据库
    • 关系数据
    • 相同结构的多个实例
    • 在应用程序进程被终止后不会丢失数据
    • 更重要的操作
  2. 坚持使用Android的共享偏好设置
    • 原始类型(booleanstringint ..)这样的简单数据只发生一次
    • 在应用程序进程被终止后不会丢失数据
    • 轻型操作
  3. 在内部/外部存储器中保留文件
    • 根据您的选择,可以是 1。 2。
    • 难以维持 1。 2。
  4. 将其保存在内存
    • 您的应用程序进程被终止时数据丢失
    • 最轻的选项
  5. 您对哪一个感兴趣?

    对于大多数情况,我会建议 1。 2。,但我仍然需要更多信息


    SQLite数据库(使用我推荐的DAO模式)

    DatabaseHelper.class

    public class DatabaseHelper extends SQLiteOpenHelper {
    
       private static final int DATABASE_VERSION = 1;
       private static final String DATABASE_NAME = "your_app_name.db";
       private static final String TABLE_MODEL_CREATE=
                                   "create table " + Model.TABLE_NAME
                                   + " ( "
                                   + Model.COLUMN_ID+ " integer primary key autoincrement, "
                                   + Model.COLUMN_SOME_INTEGER + " integer, "
                                   + Model.COLUMN_SOME_STRING  + " text "
                                   + " );"; 
    
       public DatabaseHelper(Context context)
       {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
       }
    
       // will run if there is no DB with your DATABASE_NAME
       @Override
       public void onCreate(SQLiteDatabase database)
       {
          database.execSQL(TABLE_MODEL_CREATE); 
       }
    
       // will run if there is already a DB with your DATABASE_NAME and a lower DATABASE_VERSION than this
       @Override
       public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
       {
          // execute all the updates you want
          database.execSQL(UPGRADE_1); 
          database.execSQL(UPGRADE_2);
          // ...
          onCreate(database);
       }
    }
    

    ModelDAO.class

    public class ModelDAO {
    
       private SQLiteDatabase database;
       private DatabaseHelper dbHelper; 
    
       public ModelDAO(Context context)
       {
          dbHelper = new DatabaseHelper(context);
          database = GarcomApplication.db;
       }
    
       public void open() throws SQLException
       {
          database = dbHelper.getWritableDatabase();
       }
    
       public void close()
       {
          dbHelper.close();
       }
    
       public Model createModel(Model model)
       { 
          ContentValues values = modelToContentValues(model);
    
          long insertId = database.insert(Model.TABLE_NAME, null, values); 
          return getModel(insertId);
       }
    
       public Model updateModel(Model model)
       {
          ContentValues values = modelToContentValues(model);
    
          int rowsAffected = database.update(Model.TABLE_NAME, values, Model.COLUMN_ID + " = " + model.getId(), null);
          if (rowsAffected > 0)
          {  
             return getModel(model.getId());
          }
    
          return null;
       }
    
       public void deleteModel(Model model)
       { 
          database.delete(Model.TABLE_NAME, Model.COLUMN_ID + " = " + model.getId(), null);
       }
    
       public Model getModel(long modelId)
       {
          Cursor cursor = database.query(Model.TABLE_NAME, Model.allColumns, Model.COLUMN_ID + " = " + modelId, null, null, null, null);
          cursor.moveToFirst();
          Model novoModel = cursorToModel(cursor);
          cursor.close();
          return novoModel;
       }
    
       public List<Model> getModelList()
       {
          List<Model> modelList = new ArrayList<Model>();
          Cursor cursor = database.query(Model.TABLE_NAME, Model.allColumns, null, null, null, null, null);
    
          cursor.moveToFirst();
          while (!cursor.isAfterLast())
          {
             Model model = cursorToModel(cursor);
             modelList.add(model);
             cursor.moveToNext();
          }
          cursor.close();
          return modelList;
       }
    
       private ContentValues modelToContentValues(Model model)
       {
          ContentValues values = new ContentValues();
          values.put(Model.COLUMN_SOME_INTEGER, model.getSomeInteger());
          values.put(Model.COLUMN_SOME_STRING, model.getSomeString());
    
          return values;
       }
    
       private Model cursorToModel(Cursor cursor)
       {
          Model model = new Model(cursor.getLong(0), cursor.getInt(1), cursor.getString(2));
          return model;
       }
    }
    

    Model.class

    // when you have time, read about implementing Serializable or Parcelable in your models
    // it will help you to transfer this whole object throughout activities etc
    public class Model { 
    
       public static final String TABLE_NAME = "model"; 
    
       public static final String COLUMN_ID = "id";
       public static final String COLUMN_SOME_INTEGER = "some_integer";
       public static final String COLUMN_SOME_STRING = "some_string";
       private final String[] allColumns =
       {
          Model.COLUMN_ID,
          Model.COLUMN_SOME_INTEGER,
          Model.COLUMN_SOME_STRING
       };
    
       private long id;
       private Integer someInteger;
       private String someString;
    
       // constructors, getters and setters
    
    }
    

    用法:

    ModelDAO modelDAO = new ModelDAO(someContext);
    modelDAO.open(); // opening DB connection
    
    Model newModel = new Model();
    Model persistedModel = modelDAO.createModel(newModel); // inserting a new model
    Model updatedModel= modelDAO.updateModel(persistedModel); // updating a model
    modelDAO.deleteModel(updatedModel); // deleting a model
    
    modelDAO.close(); // closing DB connection (NEVER FORGET ABOUT THIS!)
    

    <强> Shared Preferences

    // getting access to SharedPreferences
    SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE); 
    
    // reading data
    Integer yourInteger = prefs.getInteger("your_integer_name", defaultIntegerValue);  
    
    // persisting data
    SharedPreferences.Editor editor = mySharedPreferences.edit();
    editor.putInteger("your_integer_name", yourInteger);
    editor.commit();