Uroš Podkrižnik
Uroš Podkrižnik

Reputation: 8647

Android: Error: Couldn't read row 1, col 1 from CursorWindow

My app is throwing me Exception:

Error: Couldn't read row 1, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Here is my function:

static public String[] getData(Context ctx) {
    String[] result = null;
    try {
        SQLiteDatabase db = ctx.openOrCreateDatabase("mydatabase", 0,
                null);
        Cursor c = db.query("answers", new String[]{"answer"}, "id_question='1'", null, null, null, null);
        if (c.getCount() == 0) {
            return null;
        }

        result = new String[c.getCount()];
        c.moveToFirst();

        for (int i = 0; i < c.getCount(); i++) {
            result[i]=c.getString(i);
            Log.v(TAG, "Answer "+c.getString(i));//responds just on 1st time
            c.moveToNext();
        }
        c.close();
        db.close();

        return result;
    } catch (Exception e) {
        Log.e(TAG,
                "Database.getData(Context) - Error: "
                        + e.getMessage());
        return result;
    }
}

Table structure:

db.execSQL("CREATE TABLE IF NOT EXISTS answers (id INTEGER PRIMARY KEY AUTOINCREMENT, " + "id_question VARCHAR, answer VARCHAR);");

When I put in cursor for search just for column

What am I doing wrong?

I would appreciate any help. Thanks!

Upvotes: 0

Views: 160

Answers (2)

QArea
QArea

Reputation: 4981

This is your solution:

result = new String[c.getColumnCount()];
for (int i = 0; i < c.getCount(); i++) {
for(int ii=0;ii<c.getColumnCount();ii++){ 
result[ii]=c.getString(1); 
....
}

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Cusror.getCount return number of rows in Cursor. use Cursor.getColumnCount() to get columns from rows:

 result = new String[c.getColumnCount()];
 for (int i = 0; i < c.getCount(); i++) {
      for(int j=0;j<c.getColumnCount();j++){  
        result[j]=c.getString(j); << here pass column index 
      }
   ....
  }

Upvotes: 2

Related Questions