Java 从sqlite android获取所有记录

Java 从sqlite android获取所有记录,java,android,sqlite,Java,Android,Sqlite,我正在创建一个数据库,我需要从数据库中读取所有记录,但我的程序在以下语句中不断崩溃: newWord= db.getAllRecords(); 我假设getAllRecords()存在问题,因为Eclipse表明没有错误 public Cursor getAllRecords() { db = dBHelper.getWritableDatabase();//obtains the writable database return db.query(DATABASE_TABLE, new

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

newWord= db.getAllRecords();
我假设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

} 

有什么想法吗?

下面是我如何从表中获取所有内容的方法

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;
}
public ArrayList getAllegements(){
ArrayList=新建ArrayList();
//选择所有查询
String selectQuery=“SELECT*FROM”+我的表格;
SQLiteDatabase db=this.getReadableDatabase();
试一试{
Cursor Cursor=db.rawQuery(selectQuery,null);
试一试{
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
MyObject obj=新的MyObject();
//只有一列
obj.setId(cursor.getString(0));
//您可以在此处添加其他列。。
列表。添加(obj);
}while(cursor.moveToNext());
}
}最后{
尝试{cursor.close();}捕获(异常忽略){}
}
}最后{
尝试{db.close();}捕获(异常忽略){}
}
退货清单;
}
公共作废打印表数据(字符串表名称){
SQLiteDatabase db=getReadableDatabase();
Cursor cur=db.rawQuery(“SELECT*FROM”+表名称,null);
如果(cur.getCount()!=0){
cur.moveToFirst();
做{
字符串行_值=”;
对于(int i=0;i
要获取所有记录的列表,请将以下方法写入数据库类:

public ArrayList<RecentStickerModel> getAllRecentRecords() {

    ArrayList<RecentStickerModel> listStickers = new ArrayList<>();

    SQLiteDatabase db = getWritableDatabase();

    String q = "SELECT name from sqlite_master WHERE type='table' AND name='" + TABLE + "'";
    Cursor cursorTable = db.rawQuery(q, null);

    if (cursorTable != null && cursorTable.moveToFirst()) {

        String tableName = cursorTable.getString(0);
        cursorTable.close();

        if (TABLE.equals(tableName)) {

            String query = "Select * from " + TABLE ;

            Cursor cursor = db.rawQuery(query, null);

            if (cursor != null && cursor.moveToFirst()) {

                Logger.e(TAG, "cursor size:" + cursor.getCount());

                int catNameCI = cursor.getColumnIndex(KEY_CAT_NAME);
                int imagePathCI = cursor.getColumnIndex(KEY_IMAGE_PATH);
                int lastStickerTimeCI = cursor.getColumnIndex(KEY_LAST_STICKER_TIME);


                do {

                    RecentStickerModel model = new RecentStickerModel();
                    model.setCatName(cursor.getString(catNameCI));
                    model.setImgStickerPath(cursor.getString(imagePathCI));
                    model.setLastStickerTime(cursor.getString(lastStickerTimeCI));


                    listStickers.add(model);
                } while (cursor.moveToNext());

                cursor.close();
            }
        }

        db.close();
    }

    return listStickers;
}

定义崩溃?Post堆栈traceNope just me is a delay:/I在创建字符串中拼写错误列名
public void printTableData(String table_name){
    SQLiteDatabase db = getReadableDatabase();

    Cursor cur = db.rawQuery("SELECT * FROM " + table_name, null);

    if(cur.getCount() != 0){
        cur.moveToFirst();

        do{
            String row_values = "";

            for(int i = 0 ; i < cur.getColumnCount(); i++){
                row_values = row_values + " || " + cur.getString(i);
            }

            Log.d("LOG_TAG_HERE", row_values);

        }while (cur.moveToNext());
    }
}
Initialize TABLE:
    private static String TABLE="your_table_name";
public ArrayList<RecentStickerModel> getAllRecentRecords() {

    ArrayList<RecentStickerModel> listStickers = new ArrayList<>();

    SQLiteDatabase db = getWritableDatabase();

    String q = "SELECT name from sqlite_master WHERE type='table' AND name='" + TABLE + "'";
    Cursor cursorTable = db.rawQuery(q, null);

    if (cursorTable != null && cursorTable.moveToFirst()) {

        String tableName = cursorTable.getString(0);
        cursorTable.close();

        if (TABLE.equals(tableName)) {

            String query = "Select * from " + TABLE ;

            Cursor cursor = db.rawQuery(query, null);

            if (cursor != null && cursor.moveToFirst()) {

                Logger.e(TAG, "cursor size:" + cursor.getCount());

                int catNameCI = cursor.getColumnIndex(KEY_CAT_NAME);
                int imagePathCI = cursor.getColumnIndex(KEY_IMAGE_PATH);
                int lastStickerTimeCI = cursor.getColumnIndex(KEY_LAST_STICKER_TIME);


                do {

                    RecentStickerModel model = new RecentStickerModel();
                    model.setCatName(cursor.getString(catNameCI));
                    model.setImgStickerPath(cursor.getString(imagePathCI));
                    model.setLastStickerTime(cursor.getString(lastStickerTimeCI));


                    listStickers.add(model);
                } while (cursor.moveToNext());

                cursor.close();
            }
        }

        db.close();
    }

    return listStickers;
}
arrayListRecent=stickersListDatabase.getAllRecentRecords();