繁体   English   中英

为什么我的Android SQLite数据库有时无法加载数据库中的所有对象?

[英]Why does my Android SQLite DB sometimes fail to load all objects in the DB?

我正在使用SQLite存储一些对象,然后将这些对象显示在列表视图中。 当活动启动时,我只需获取数据库中的所有对象,然后将结果填充到列表视图中。 偶尔(大约5%的时间),我注意到并非所有结果都已从数据库返回。 我只被1个结果害羞,并且,如果我重新加载活动,丢失的结果将正确加载。

在尝试查找问题时,我记录了查询后的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中做的有些不对劲。 我感谢您可以提供的任何建议。 谢谢!

尝试在此处使用以下代码,我正在创建列表,并将此列表添加到您的listview中。

ArrayList results= new ArrayList();
    SQLiteDatabase myDB = this.openOrCreateDatabase("dummy.db", SQLiteDatabase.OPEN_READWRITE, null);
         try{
                Cursor c = myDB.rawQuery("select a from xyz", null); 
                int Column1 = c.getColumnIndex("a");


            // Check if our result was valid.
            c.moveToFirst();
            if (c != null) {
                int i = 0;
                // Loop through all Results
                do {
                    i++;
                 String a= c.getString(Column1);

                 results.add(a);
                    } while (c.moveToNext()); 
                     } 


            } catch (SQLiteException e) { 
                e.printStackTrace();
            } finally { 
                 if (myDB != null) 
                      myDB.close(); 
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM