Android 中尝试使用游标从 SQLite 获取整数值时出错

Error when trying to use cursor to get integer values from SQLite in Android

我正在尝试获取整数值,以使用游标从 SQLlite 显示在列表视图中,但它显示以下错误:

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

这是我的代码

MyItems.java:

public  ArrayList<SalesItemInformationLV> retrieveAllForlist(Context c)
{
    ArrayList<SalesItemInformationLV> items = new ArrayList<SalesItemInformationLV>();

    Cursor myCursor;
    String mystring = "";

    MyDbAdapter db = new MyDbAdapter(c);
    db.open();
    //contactIdList.clear();
    //contactList.clear();
    myCursor = db.retrieveAllEntriesCursor();

    if (myCursor != null && myCursor.getCount() > 0)
    {
        myCursor.moveToFirst();
        do {
            contactIdList.add(myCursor.getInt(db.COLUMN_KEY_ID));
            items.add(new SalesItemInformationLV(myCursor.getString(db.COLUMN_NAME_ID), myCursor.getInt(db.COLUMN_QTYSOLD_ID)));
        } while (myCursor.moveToNext());
    }
    db.close();

    return items;
}

MyDbAdapter.java:

private SQLiteDatabase _db;
private final Context context;

public static final String KEY_ID = "_id";
public static final int COLUMN_KEY_ID = 0;
public static final String ENTRY_NAME = "entry_name";
public static final int COLUMN_NAME_ID = 1;
public static final String ENTRY_QTYSOLD = "entry_qtysold";
public static final int COLUMN_QTYSOLD_ID = 4;

private MyDBOpenHelper dbHelper;
//private MyDBOpenHelper dbHelper2;

public MyDbAdapter(Context _context)
{
    this.context = _context;
    
    //step 16 - create MyDBOpenHelper object
    //constructor
    dbHelper = new MyDBOpenHelper(context, DATABASE_NAMEA, null, DATABASE_VERSION);
    //constructor
    //dbHelper2 = new MyDBOpenHelper(context, DATABASE_NAME2, null, DATABASE_VERSION);
}

public Cursor retrieveAllEntriesCursor() {
    //step 21 - retrieve all records from table
    Cursor c = null;
    try {
        c = _db.query(DATABASE_TABLE, new String[] {KEY_ID, ENTRY_NAME}, null, null, null, null, null);

    }
    catch (SQLiteException e)
    {
        Log.w(MYDBADAPTER_LOG_CAT, "Retrieve fail!");
    }

    return c;
}

我怀疑错误来自 MyItems.java,但我很难弄清楚错误是什么。

似乎您只从数据库中提取了 2 列(KEY_ID, ENTRY_NAME),而在读取时您期望得到 3 列。

 c = _db.query(DATABASE_TABLE, new String[] {KEY_ID, ENTRY_NAME}, null, null, null, null, null);

您正在尝试从第 4 列获取值,但出现错误。

public static final int COLUMN_QTYSOLD_ID = 4;

在你的数据库助手中使用这个方法class

public Cursor getalldata() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery(" select * from " + TABLE_Name, null);

    return res;


}

** 并在您想从数据库中获取数据的地方调用此方法 table **

 public void getdata(){

    Cursor res = db.getstafdata(); //db id an object of database helper //class
    if (res.getCount() == 0) {
        Toast.makeText(getApplicationContext(),
                "no data", Toast.LENGTH_LONG).show();

    } else {

        StringBuffer stbuff = new StringBuffer();
        while (res.moveToNext()) {

            detail.add(new doctor_details(res.getString(1),res.getString(2),res.getString(3)));

        }


}

}