android.database.CursorIndexOutOfBoundsException:请求索引10,大小为10

时间:2012-09-14 06:58:09

标签: android sqlite offset indexoutofboundsexception

我正在学习使用SQLite操作的android。以下代码会导致CursorIndexOutOfBoundsException,但我不知道原因:

for (int i = 0; i < cursor.getCount(); i++) {
    cursor.move(i);
    String username = cursor.getString(1);
    Log.i("username", username);
}

日志和例外:

09-13 12:39:18.317: INFO/row count:(3578): 10
09-13 12:39:18.317: INFO/username(3578): aaa_0
09-13 12:39:18.317: INFO/username(3578): aaa_1
09-13 12:39:18.317: INFO/username(3578): aaa_3
09-13 12:39:18.317: INFO/username(3578): aaa_6
09-13 12:39:18.337: ERROR/AndroidRuntime(3578): FATAL EXCEPTION: main
    java.lang.RuntimeException: 
    Unable to start activity ComponentInfo{com.example/com.example.MyActivity}: 
    android.database.CursorIndexOutOfBoundsException: 
    Index 10 requested, with a size of 10

您可以看到行数为10,但在获取第5行时会抛出异常。

完整的代码是:

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

public class MyActivity extends Activity {

    private SQLiteDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        createTables();
        insertData();
        queryData();
    }


    private void queryData() {
        Cursor cursor = db.query("users", new String[]{"_id", "username", "password"}, null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            Log.i("row count: ", "" + cursor.getCount());
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.move(i);
                String username = cursor.getString(1);
                Log.i("username", username);
            }
        }
    }

    private void insertData() {
        for (int i = 0; i < 10; i++) {
            String sql = "insert into users(username, password) values('aaa_" + i + "','123_" + i + "')";
            db.execSQL(sql);
        }
    }

    private void createTables() {
        this.deleteDatabase("temp.db");
        db = this.openOrCreateDatabase("temp.db", MODE_PRIVATE, null);
        String sql = "create table users(_id integer primary key autoincrement, username text, password text)";
        db.execSQL(sql);
    }

}

4 个答案:

答案 0 :(得分:3)

这是因为move(int offset)执行此操作:

  

将光标移动相对数量,向前或向后,从当前位置

答案 1 :(得分:2)

你不应该使用move api。这是通过偏移移动位置。而是使用

Cursor.moveToPosition

答案 2 :(得分:2)

试试这个......

      cursor.moveToFirst();
    for (int i = 0; i < cursor.getCount(); i++) 
         {           
            String username = cursor.getString(1);
            Log.i("username", username);
            cursor.moveToNext() 
         }

或根据您的代码......

private void queryData() {
        Cursor cursor = db.query("users", new String[]{"_id", "username", "password"}, null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            Log.i("row count: ", "" + cursor.getCount());
            for (int i = 0; i < cursor.getCount(); i++) {                
                String username = cursor.getString(1);
                Log.i("username", username);
                cursor.moveToNext() 
            }
        }
    }

答案 3 :(得分:2)

使用do while循环而不是for循环。

if (cursor.getCount() != 0) 
{
    if (cursor.moveToFirst()) 
    {
        do 
        {
             // cursor.getString(cursor.getColumnIndex("username")));               
        } while (cursor.moveToNext());
    }
}