更有效的方式来存储在Android的数据库中的信息 [英] More efficient ways to store information in Android Database
问题描述
有一段时间了,我一直在使用JSON格式存储在Android应用程序的信息。
For a while now, I've been using JSON formatting to store information in Android applications.
然而,这有时是混乱的,我觉得这是在某些方面效率低下。我只是有一个更普遍的问题:在那里存储在Android应用程序信息的更有效的方法
However, this is sometimes messy and I feel like it is inefficient in some aspects. I just have a more general question: are there any more efficient ways to store information in Android applications?
推荐答案
可以的将信息存储在Android的在4种方式:
- 坚持一个自定义的数据库
- 关系数据
- 的结构相同的多个实例
- 应用程序进程被终止后不会丢失数据
- 较重的操作
- 简单的数据,如原始类型(
布尔
,字符串
,INT
..)只发生一次 - 应用程序进程被终止后不会丢失数据
- 补光灯操作
- Simple data like primitive types (
boolean
,string
,int
..) that only occurs once - Don't lose data after the app process is killed
- Light operations
- 根据您的选择,可以像 1 或 2
- 难以维持比 1 或 2
- Depending on your choice, can be like 1. or 2.
- Harder to maintain than 1. or 2.
- 数据时,您的应用程序进程被杀掉丢失
- 最轻的所有选项
哪一个是你感兴趣的?
Which one are you interested in?
我推荐的 1 或 2 在大多数情况下,但我仍然需要更多的信息。
I would recommend 1. or 2. for most cases, but i still need more info
SQLite数据库(使用我推荐一个DAO模式)
SQLite Database (using a DAO pattern that i recommend)
的 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();
这篇关于更有效的方式来存储在Android的数据库中的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!