【问题标题】:Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it无法从 CursorWindow 读取第 0 行第 5 列。确保在从光标访问数据之前正确初始化光标
【发布时间】:2020-05-18 20:54:33
【问题描述】:

原木猫

原因:java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行第 5 列。确保在从光标访问数据之前正确初始化光标。 在 android.database.CursorWindow.nativeGetString(Native Method) 在 android.database.CursorWindow.getString(CursorWindow.java:465) 在 android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 在 com.example.workhours.DataBaseHelper.ViewAllNotes(DataBaseHelper.java:90) 在 com.example.workhours.MainActivity.ViewAllNotes(MainActivity.java:55) 在 com.example.workhours.MainActivity.onCreate(MainActivity.java:37)

public  ArrayList<newNote> ViewAllNotes() {
    ArrayList<newNote> arrayList = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT NOTEMEMOS FROM " + TABLE_NAME, null);

    while(cursor.moveToNext()){
        String notes = cursor.getString(5);
        newNote newNote = new newNote(notes);
        arrayList.add(newNote);
    }
    return arrayList;
}

查询

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, DATE TEXT, TIMESHIFTSTART INTEGER, TIMESHIFTENDS TEXT, NOTES TEXT, NOTEMEMOS TEXT)");
}

【问题讨论】:

    标签: java sqlite android-studio android-sqlite android-sql


    【解决方案1】:

    您的光标有一列SELECT NOTEMEMOS,但您正尝试使用getString(5) 读取第六列。将其替换为 getString(0) 以读取唯一的列值。

    【讨论】:

      【解决方案2】:

      在获取任何数据之前尝试将光标移至第一个,并且您只选择数据库中的列,但您要求的是第 5 个。

      public  ArrayList<newNote> ViewAllNotes() {
      ArrayList<newNote> arrayList = new ArrayList<>();
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery("SELECT NOTEMEMOS FROM " + TABLE_NAME, null);
      
      cursor.moveToFirst();
      
      newNote newnote = new newNote(cursor.getString(0));
      arrayList.add(newnote);
      
      while(cursor.moveToNext()){
          String notes = cursor.getString(0);
          newNote newnote = new newNote(notes);
          arrayList.add(newnote);
      }
      return arrayList;
      }
      

      【讨论】:

      • 请注意,这将跳过第一个结果行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多