java.lang.IllegalStateException: Couldn't read row 0, col 10 from CursorWindow

Please check following code:

List<Database> zaznamy = new ArrayList<Database>();
    String selectQuery = "SELECT X  FROM Data WHERE LEVEL_1 =-24 AND LEVEL_2 =-48 AND LEVEL_3 =-55 AND LEVEL_4 =0";
    File dbfile = new File("/sdcard/rtls/Databases/"+DBName ); 
    SQLiteDatabase  db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    Cursor cursor = db.rawQuery(selectQuery,null);

    String bodX="empty";
    if (cursor.moveToFirst()) 

        {
        do {
            Database zaznam = new Database();
            zaznam.setX(Integer.parseInt(cursor.getString(10)));
            zaznamy.add(zaznam);
        } while (cursor.moveToNext());

        for (Database cn : zaznamy) 
        {
            Log.d("X: ", Integer.toString(cn.getX()));
            bodX = (cn.getX()+ "//").toString();
            Log.d("X", bodX);
        }
    }

It says it Couldn't read row 0, col 10 from CursorWindow. I´ve tested the database with SQLite browser. Database has exactly 1 X at column 10, row 0. SQL query is working correctly I believe. Can someone tell me, why it cant be read? Or where is mistake?

EDIT: corrected the code to:

do {
            Database zaznam = new Database();
            zaznam.setX(cursor.getInt(10));
            zaznamy.add(zaznam);
        } while (cursor.moveToNext());

because of integer value of 10th column, but still no luck. Same error

Answers


Your query has only one column in projection - column 'X'. That column has index 0 in query projection so to make your code work change your loop to looks like this:

do {
        Database zaznam = new Database();
        zaznam.setX(cursor.getInt(0));
        zaznamy.add(zaznam);
} while (cursor.moveToNext());

To avoid that kind of problems in the future use following:

cursor.getInt(cursor.getColumnIndexOrThrow("X");

Remember that column index is related to query projection and not the sequence of columns in your database, so for example when you write "select B A C from SOME_TABLE" column B will have index 0 and column A will have index 1 etc. even if in your database they are in alphabetical order A B C.


I was having the same issue. For me. A simple work around may be:

// query database and populate an ArrayList
private void getDataFromDatabase() {
    Cursor cursor = dbConnector.getSomeData();
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        for(int i = 0; i < cursor.getCount(); i++) {
            // get column data from database and add it to ArrayList
            anArrayList.add(cursor.getString(cursor.getColumnIndexOrThrow("Attr")));

            cursor.moveToNext();
        } // end for
    } // end if
    dbConnector.close();
} // end getDataFromDatabase

WHERE "Attr" is the Attribute/column name you wish to query and the anArrayList is the ArrayList you want to store the data in.


Need Your Help

Procedure entry point could not be found?

c++ visual-studio-2010 linker ffmpeg dynamic-linking

I've encountered a strange problem. After updating to the latest ffmpeg headers/lib/dll I keep getting the error:

android app keeps crashing audioEffect error code -1

android crash audioeffect

I put some code in for continuous generation and playback of a sine wave inside my main activity for my app and created a PresetReverb object to test it out on the sine wave. But, every time I laun...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.