从sqlite android获取所有记录

Ed *_*ing 9 java sqlite android

我正在创建一个数据库,我需要从数据库中读取所有记录,但是我的程序在这个语句中不断崩溃:

newWord= db.getAllRecords();
Run Code Online (Sandbox Code Playgroud)

我假设getAllRecords()存在问题,因为Eclipse表示没有错误.

 public Cursor getAllRecords() {
 db = dBHelper.getWritableDatabase();//obtains the writable database
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,KEY_WORD}, null, null, null,      null, null);//the query used to obtain all records form the table

} 
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Joe*_*oel 14

这是我从表中获取所有内容的方法..

public ArrayList<MyObject> getAllElements() {

    ArrayList<MyObject> list = new ArrayList<MyObject>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + MY_TABLE;

    SQLiteDatabase db = this.getReadableDatabase();
    try {

        Cursor cursor = db.rawQuery(selectQuery, null);
        try {

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    MyObject obj = new MyObject();
                    //only one column
                    obj.setId(cursor.getString(0));

                    //you could add additional columns here..

                    list.add(obj);
                } while (cursor.moveToNext());
            }

        } finally {
            try { cursor.close(); } catch (Exception ignore) {}
        }

    } finally {
         try { db.close(); } catch (Exception ignore) {}
    }

    return list;
}
Run Code Online (Sandbox Code Playgroud)