java.lang.IllegalStateException:无法从CursorWindow读取第0行第3列

时间:2016-09-08 07:11:27

标签: java android sqlite

我正在创建一个简单的应用程序。但是,我的应用程序崩溃,我收到错误 -

  

java.lang.IllegalStateException:无法读取第0行,第3列   CursorWindow。确保之前正确初始化了Cursor   从中访问数据。

现在我在m表中有四行 - Id name age qtytoBuy 。请告诉我在哪里弄乱?我在SO上发现了类似的问题,但它对我没有帮助。

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;
    }

}

0 个答案:

没有答案