1
我使用SQLite来存储一些对象,然后显示在列表视图中。当活动启动时,我只需抓取数据库中的所有对象,并使用结果填充列表视图。偶尔(约5%的时间),我注意到并非所有结果都从数据库返回。我一直只对1结果感到害羞,而且,如果我重新加载活动,则会正确加载缺失的结果。为什么我的Android SQLite数据库有时无法加载数据库中的所有对象?
在试图追踪问题时,我记录了Cursor对象计数后查询并注意到当结果丢失时,它也从Cursor对象中丢失(所以问题不在于使返回的对象膨胀) 。有没有人有任何这个问题的经验?
下面是我存储在我的数据库中的一个对象的示例。
public class MyObject {
public static final String DB_TABLE = "object";
public static final int DB_VERSION = 1;
public static final String COL_ID = "id";
public static final String COL_TITLE = "title";
public static final String COL_SUBTITLE = "subtitle";
public static final String COL_DESCRIPTION = "description";
public static final String COL_START_TIME = "start_time";
public static final String COL_END_TIME = "end_time";
public static final String COL_IMAGE1 = "background_image";
public static final String COL_IMAGE2 = "icon_image";
public static final String COL_EXTRAS = "extras";
public static final String DB_CREATE = String.format("create table if not exists %1$s (%2$s text primary key, %3$s text, %4$s text, %5$s text, %6$s integer, %7$s integer, %8$s text, %9$s text, %10$s integer);",
DB_TABLE, COL_ID, COL_TITLE, COL_SUBTITLE, COL_DESCRIPTION, COL_START_TIME, COL_END_TIME, COL_IMAGE1, COL_IMAGE2, COL_EXTRAS);
public static final String [] DB_COLUMNS = new String [] {COL_ID, COL_TITLE, COL_SUBTITLE, COL_DESCRIPTION, COL_START_TIME, COL_END_TIME, COL_IMAGE1, COL_IMAGE2, COL_EXTRAS};
public String Id;
public String Title;
public String Subtitle;
public String Description;
public long StartTime;
public long EndTime;
public String Image2;
public String Image2;
public int Extras;
public Event() {}
public static DataBaseHelper getDBHelper(Context context) {
return new DataBaseHelper(context, QmobixConferenceApplication.DB_NAME, DB_VERSION, DB_CREATE, DB_TABLE, COL_ID);
}
}
而且DataBaseHelper有关块...
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DataBaseHelper";
private SQLiteDatabase mDB;
private String mSQL;
private String mDBName;
private String mTableName;
private String mKeyId;
private boolean isClosed = false;
public DataBaseHelper(Context context, String DBName, int version, String sql, String tableName, String keyId) throws SQLiteException {
super(context, DBName, null, version);
this.mSQL = sql;
this.mDBName = DBName;
this.mTableName = tableName;
this.mKeyId = keyId;
try {
mDB = this.getWritableDatabase();
mDB.execSQL(mSQL);
} catch(SQLiteException ex) {
Log.e(TAG, String.format("Could not create and/or open the database [ %1$s ]", mDBName), ex);
throw ex;
}
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(mSQL);
} catch(SQLException ex) {
Log.e(TAG, String.format("Could not create table according to SQL [ %1$s }", mSQL), ex);
}
}
@Override
public synchronized void close() {
super.close();
if(mDB != null) {
mDB.close();
}
isClosed = true;
}
public Cursor get(String [] columns, String orderBy) {
return mDB.query(mTableName, columns, null, null, null, null, orderBy);
}
}
现在为例加载从我的数据库对象。
DataBaseHelper db = null;
Cursor cursor = null;
try {
db = MyObject.getDBHelper(mContext);
cursor = db.get(MyObject.DB_COLUMNS, String.format("%1$s ASC", MyObject.COL_START_TIME));
} catch(Exception ex) {
// bad news
} finally {
if(cursor != null) { cursor.close(); }
if(db != null) { db.close(); }
}
我知道很多东西需要阅读。希望以前有人看到过,这只是我在DataBaseHelper中做了一些工作。我很感谢您提供的任何建议或建议。谢谢!