在Android中,最简单/最简单的方法是存储一个简单的连接值元组,f.ex。 Date date
,String name
,int count
?
使用SharedPreferences
,您可以保留递增ID并将值存储为
editor = context.getSharedPreferences().edit();
editor.putString("name" + ID, name);
editor.putLong("date" + ID, date.getTime());
editor.putInt("count" + ID, count);
editor.apply();
ID += 1;
但解决方案应扩展到数千个条目。这实际上是关于存储非常简单的数据,因此像Realm这样的数据库可能有点过分。 SQLite似乎需要很多样板。
哪种方法需要最少的编程工作/代码行?
更新:也许Google Room可行吗?
答案 0 :(得分:3)
对于简单数据,最简单的方法是创建Object类并将值放入其中,然后使用Gson将类转换为JSON,并将您的JSON保存在SharedPreferences中作为单个值。然后,您可以从SharedPreferences中检索JSON,并使用Gson转换为您的对象。
将对象转换为JSON:
String jsonString = new Gson().toJson(yourModel);
将JSON从String转换为Object:
YourObject myModel = new Gson().fromJson(jsonString, YourObject.class);
答案 1 :(得分:1)
这是一个非常基本的SQLiteOpenHelper
示例,它有一个表,可以选择all并创建一行
public class DbHelper extends SQLiteOpenHelper {
public static class Entry {
public long id;
public String value;
}
public static final int VERSION = 1;
public static final String NAME = "MyDb";
private static final String TABLE_ENTRY = "MyTable";
private static final String KEY_ENTRY_ID = "id";
private static final String KEY_ENTRY_VALUE = "value";
private static final String SQL_CREATE_ENTRY_TABLE = "CREATE TABLE "+ TABLE_ENTRY + "(" +
KEY_ENTRY_ID + " INTEGER PRIMARY KEY, " +
KEY_ENTRY_VALUE + " TEXT" +
")";
public DbHelper(Context context) {
super(context, NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRY_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public List<Entry> selectAll(){
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_ENTRY, null);
List<Entry> output = new ArrayList<>();
if(cursor.moveToFirst()){
while(!cursor.isAfterLast()){
Entry entry = new Entry();
entry.id = cursor.getLong(cursor.getColumnIndex(KEY_ENTRY_ID));
entry.value = cursor.getString(cursor.getColumnIndex(KEY_ENTRY_VALUE));
output.add(entry);
cursor.moveToNext();
}
}
return output;
}
public long createEntry(Entry entry){
SQLiteDatabase db = getWritableDatabase();
try {
ContentValues values = new ContentValues();
values.put(KEY_ENTRY_VALUE, entry.value);
return db.insert(TABLE_ENTRY, null, values);
}catch (Exception e){
return -1;
}
}
}
答案 2 :(得分:0)
查看Android的JDXA ORM,以便在SQLite数据库中轻松存储对象数据。它可以帮助您避免编写样板代码,如下所示。免责声明:我是JDXA ORM的架构师。
JDXA非侵入式,灵活且对POJO友好。在这种特殊情况下,您可以按如下方式声明POJO类SampleEntity:
public class SampleEntity {
private int id; // auto-generated by the database
private String name;
private Date date;
private long count;
/**
* Default no-arg constructor needed for JDXA
*/
public SampleEntity() {
}
public SampleEntity(String name, Date date, long count) {
this.name = name;
this.date = date;
this.count = count;
}
// Other constructors and accessor methods omitted
此类的对象关系映射(ORM)规范可以声明性地定义如下:
CLASS SampleEntity
PRIMARY_KEY id
SQLMAP FOR id SQLTYPE 'INTEGER PRIMARY KEY AUTOINCREMENT'
RDBMS_GENERATED id
;
上面的RDBMS_GENERATED规范意味着底层数据库将自动生成指定属性(例如id)的值。
JDXA为CRUD操作提供了简单的API。以下是此类API调用的一些示例(例如,插入,查询,删除):
JDXS jdxHandle = jxResource.getJDXHandle();
String sampleEntityClassName = SampleEntity.class.getName();
try {
// Delete all existing SampleEntity objects from the database.
jdxHandle.delete2(sampleEntityClassName, null, JDXS.FLAG_DEEP);
// Create and save some SampleEntity objects. id will be automatically generated by the database.
SampleEntity entity = new SampleEntity("Name1", new Date(), 10);
jdxHandle.insert(entity, 0, null);
entity = new SampleEntity("Name2", new Date(), 20);
jdxHandle.insert(entity, 0, null);
// Retrieve all the SampleEntity objects. They will come initialized with the database generated id values.
List queryResults = jdxHandle.query(sampleEntityClassName, null, JDXS.ALL, JDXS.FLAG_DEEP, null);
...
} catch (Exception ex) {
...
}
您不需要编写和维护任何SQL代码。此外,避免了许多用于数据传输的样板代码。此外,JDXA将自动生成所需的数据库架构。