SQLite的空光标错误? [英] Sqlite empty cursor error?

查看:92
本文介绍了SQLite的空光标错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,此代码可以很好地工作.但是,如果光标"为空,则main.class中存在错误.我尝试了很多事情.但是,我没有成功.请帮忙解决问题.

Normaly, this code working very well. But, if "cursor" is empty, there is an error in main.class. I tried a lot of thing. But, I didn't success. Please help to solve way.

---- database.class ----

---- database.class ----

  public List<Integer> count_a() {
    List<Integer> list = new ArrayList<Integer>();

    String selectQuery = "select book, count(date) from myTAB WHERE (date>'" + 0 + "') group by book"; 
// if result is empty, there is an error in main.class.
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            list.add(Integer.parseInt(cursor.getString(1)));
        }
        while (cursor.moveToNext());
    }

    cursor.close();
    db.close();
    return list;
}

---- main.class ----

---- main.class ----

 private void load_data() {
    Database db = new Database(getApplicationContext());

    List<Integer> co_a = db.count_a(); // error is here

    Integer[] co_b = new Integer[co_a.size()];
    co_b = co_a.toArray(co_b);

    List_Row adapter = new List_Row(this, co_b);
    ListView lv = (ListView) findViewById(R.id.lvStat);
    lv.setAdapter(adapter);
}

失败1

    if (cursor.moveToFirst()) {
        do {
            if (cursor.getCount() == 0 || cursor == null) {
                list.add(0);
            } else {
                list.add(Integer.parseInt(cursor.getString(1)));
            }
        }
        while (cursor.moveToNext());
    }

失败2

  if (cursor.moveToFirst()) {
        do {
            if(cursor.getCount() == 0){
                list.add(0);
            }else {
                list.add(Integer.parseInt(cursor.getString(1)));
            }
        }
        while (cursor.moveToNext());
    }

失败3

    if (cursor != null && cursor.moveToFirst()) {
        do {
            if (cursor.getCount() > 0) {
                list.add(Integer.parseInt(cursor.getString(1)));
            } else {
                list.add(0);
            }
        }
        while (cursor.moveToNext());
    }

失败4

    if (cursor.moveToFirst()) {
        do {
            if (cursor != null && cursor.getCount() > 0) {
                list.add(Integer.parseInt(cursor.getString(1)));
            } else {
                list.add(0);
            }
        }
        while (cursor.moveToNext());
    }

失败5

if (cursor.moveToFirst()) {
        do {
            list.add(Integer.parseInt(cursor.getString(1)));
        }
        while (cursor.moveToNext());
    }
    if (cursor.isBeforeFirst()){
        do {
            list.add(0);
        }
        while (cursor.moveToNext());
    }

推荐答案

如果游标不包含任何数据,则 moveToFirst()返回false.因此,在第一个代码段中,如果有条件,将任何特殊的空游标处理添加到 if(cursor.moveToFirst()) else 分支中:

If the cursor contains no data, moveToFirst() returns false. So add any special empty cursor handling to an else branch of your if (cursor.moveToFirst()) conditional in your first snippet:

if (cursor.moveToFirst()) {
    do {
        list.add(Integer.parseInt(cursor.getString(1)));
    }
    while (cursor.moveToNext());
} else {
    // whatever you'd like to do in case of no data
}

这篇关于SQLite的空光标错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆