0

I am creating a simple app. But , my app crashes and I am getting an error -

java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Still now I have four rows in m table - Id, name, age, qtytoBuy. Please tell me where am I messing up ? I have find similar questions on SO But it did not help me.

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow - Android sqlite issue

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

DatabaseHandler.java

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

    //Database Version
    private static final int DATABASE_VERSION = 1;
    //Database Name
    private static final String DATABASE_NAME = "Test";
    //Table Name
    private static final String TABLE_TEST = "TestTable";
    //Column Name
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_AGE = "age";
    private static final String KEY_QTYBUY= "qtyBuy";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //Create Table
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_TEST + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_AGE + " TEXT," + KEY_QTYBUY + " TEXT"+ ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEST);
        onCreate(db);
    }

    //Insert Value
    public void adddata(Context context,String movieId,String songId, String qtyToBuy) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, movieId);
        values.put(KEY_AGE, songId);
        values.put(KEY_QTYBUY, qtyToBuy);
        db.insert(TABLE_TEST, null, values);
        db.close();
    }

    //Get Row Count
    public int getCount() {
        String countQuery = "SELECT  * FROM " + TABLE_TEST;
        int count = 0;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        if(cursor != null && !cursor.isClosed()){
            count = cursor.getCount();
            cursor.close();
        } 
        return count;
    }

    //Delete Query
    public void removeFav(int id) {
        String countQuery = "DELETE FROM " + TABLE_TEST + " where " + KEY_ID + "= " + id ;
        SQLiteDatabase db = this.getReadableDatabase();
        db.execSQL(countQuery);
    }

    //Get FavList
    public List<FavoriteList> getFavList(){
        String selectQuery = "SELECT  * FROM " + TABLE_TEST;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        List<FavoriteList> FavList = new ArrayList<FavoriteList>();
        if (cursor.moveToFirst()) {
            do {
                FavoriteList list = new FavoriteList();
                list.setId(Integer.parseInt(cursor.getString(0)));
                list.setName(cursor.getString(1));
                list.setAge(cursor.getString(2));
                list.setQtyBuy(cursor.getString(3));
                FavList.add(list);
            } while (cursor.moveToNext());
        }
        return FavList;
    }

}
Community
  • 1
  • 1
SOURAV
  • 73
  • 8
  • 1
    Also post Url of similar questions(May be 1 or 2 that is identical with your question) that didn't help you. Otherwise someone will make this question duplicate. – Pravin Divraniya Sep 08 '16 at 07:17
  • 1
    Have you tried with cursor.getString(cursor.getColumnIndex("Name of your column")) instead of cursor.getString(0) ? – Pravin Divraniya Sep 08 '16 at 07:34
  • @PravinDivraniya Thanks for your help. After using what you said, I got an error . http://stackoverflow.com/questions/17441927/android-sqlite-issue-table-has-no-column-named - post solved my problem. – SOURAV Sep 08 '16 at 07:44

0 Answers0