1 - 多次扩展 SQLiteOpenHelper 是错误的并且无法工作。 2 - 有一个扩展 SQLiteOpenHelper 以处理 (CRUD) 7 个不同类的大类很奇怪。
有例子吗?在没有大量依赖类的情况下将许多实体添加到我的数据库中?
我似乎无法在 Google 上找到大于 2 或 3 个表的示例。 拥有一个非常大的 SQLiteOpenHelper 以及我所有实体的保存方法是很糟糕的。 该项目也不允许使用 ORM。
有任何帮助/指示吗?
最佳答案
Having a very big SQLiteOpenHelper with save methods for all of my entities is horrible.
你认为好的指定应用程序会只有几行代码吗?
在 Android 中,从 SQLiteOpenHelper
扩展是尽可能最好的选择。它包装了所有必需的逻辑,在我看来使用它非常舒服。但都需要正确实现。
Extending many times the SQLiteOpenHelper is wrong and wont work
SQLiteOpenHleper
很可能没有问题,但我认为实现和使用有误。
你的意思是,ORM 框架例如 ORMLite
是不允许的,所以我的建议是使用 SQLiteOpenHelper
并进行正确和干净的实现。
更新:
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?
我在评论中的意思是这是实现问题。你真的不需要 7 个表以上的 7 个保存方法。这是我的第一个想法。如何创建一种通用方法?
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();
}
}
}
注意:这是第一个概念,刚刚专门为您编写,因此需要更新:考虑方法的参数 --> 如何做出更好的选择、事务的使用、数据源作为单例,对插入、更新进行一些性能测试。一切都取决于应用程序的特性。
关于java - 在 Android SQLite 中处理 "complex"数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345187/