Android 从sqlite数据库获取数据时不返回任何记录

Android 从sqlite数据库获取数据时不返回任何记录,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,在数据库中成功插入了一行。我导出sqlite数据库文件,它向我显示记录。现在我正试图从数据库中获取该记录。数据库只有一条记录 String containerData = ""; SQLiteDatabase db= this.getReadableDatabase(); Cursor cursor= db.rawQuery("select "+ " * " + " from "+ TABLE_NAME, null); if(cursor != null)

在数据库中成功插入了一行。我导出
sqlite
数据库文件,它向我显示记录。现在我正试图从数据库中获取该记录。数据库只有一条记录

 String containerData = "";

        SQLiteDatabase db= this.getReadableDatabase();
        Cursor cursor= db.rawQuery("select "+ " * " + " from "+ TABLE_NAME, null);

 if(cursor != null){
            cursor.moveToFirst();    
            while(cursor.moveToNext()){
            containerData= cursor.getString(0);
           }

        cursor.close();
        db.close();
我尝试过使用db.query(),也尝试过删除while循环,并使用了以下方法

  containerData= cursor.getString(0);
但这给了我以下错误信息

方法引发了“android.database.CursorIndexOutOfBoundsException” 例外


我的表中总共有5列

如果只检查游标的null值,则还需要检查该游标中是否有任何值:

if (cursor != null && !cursor.isAfterLast()){
...
}

如果只检查游标的null值,则还需要检查该游标中是否有任何值:

if (cursor != null && !cursor.isAfterLast()){
...
}

请尝试以下代码:

 String containerData = "";

 SQLiteDatabase db= this.getReadableDatabase();
 Cursor cursor= db.rawQuery("select "+ " * " + " from "+ TABLE_NAME, null);

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

    while (!cursor.isAfterLast()){
        containerData= cursor.getString(0);
        cursor.moveToNext();
        Log.d("ShowData->", containerData);
    }
 }

 cursor.close();
 db.close();

请尝试以下代码:

 String containerData = "";

 SQLiteDatabase db= this.getReadableDatabase();
 Cursor cursor= db.rawQuery("select "+ " * " + " from "+ TABLE_NAME, null);

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

    while (!cursor.isAfterLast()){
        containerData= cursor.getString(0);
        cursor.moveToNext();
        Log.d("ShowData->", containerData);
    }
 }

 cursor.close();
 db.close();

!cursor.isAfterLast()返回true,当我导出文件时,它会在表中显示记录!cursor.isAfterLast()返回true,当我导出文件时,它会在表中显示记录。首先,我建议从原始sql切换到更快速、更友好的方法ORM,android领域没有sql,更快速、更易于维护最简单的方法是
cursor cursor=db.rawQuery(…);如果(cursor.moveToFirst()){do{/*使用cursor*/}而(cursor.moveToNext();}cursor.close()执行某些操作
不需要对rawquery进行空检查,我不建议使用ORM,因为使用fx CursorAdapter的游标内存占用较低。相比将所有对象加载到POJO并使用CustomAdapter@Selvin@Remario:如果我使用这个
db.query(TABLE_NAME,newstring[]{COLUMN_NAME}),效率会更高吗,null,null,null,null,null,null)可能的重复首先,我建议从原始sql切换到更快速、更友好的方法ORM,android领域没有sql,更快速、更易于维护最简单的方法是
Cursor Cursor=db.rawQuery(…);如果(cursor.moveToFirst()){do{/*使用cursor*/}而(cursor.moveToNext();}cursor.close()执行某些操作
不需要对rawquery进行空检查,我不建议使用ORM,因为使用fx CursorAdapter的游标内存占用较低。相比将所有对象加载到POJO并使用CustomAdapter@Selvin@Remario:如果我使用这个
db.query(TABLE_NAME,newstring[]{COLUMN_NAME}),效率会更高吗,null,null,null,null,null,null)可能重复的