Android SQLite数据库:CursorIndexOutOfBoundsException

Android SQLite数据库:CursorIndexOutOfBoundsException,android,sqlite,Android,Sqlite,我正在实现一个应用程序,我喜欢使用SQLite数据库来存储用户的一些信息。我需要在数据库中输入id=0,以便每次更新同一行 user.clean() = Clear all the values stored in database (row with id = 0); user.save() = update the values in the database (row with id = 0); user.load() = get all the values from the datab

我正在实现一个应用程序,我喜欢使用SQLite数据库来存储用户的一些信息。我需要在数据库中输入id=0,以便每次更新同一行

user.clean() = Clear all the values stored in database (row with id = 0);
user.save() = update the values in the database (row with id = 0);
user.load() = get all the values from the database (row with id = 0);
我的问题是:应用程序不从数据库中读取任何内容,并且我在logcat中没有异常

我的班级:

/**
* This class is used to manage the user information.
*/
public class User extends SQLiteOpenHelper {

    private static final String LOCAL_DB_NAME = "local_db";
    private static final int LOCAL_DB_VERSION = 1;
    public static final String USER_DB_TABLE = "local_user";
    public static final String[] USER_ALL_COLUMNS = {"id", "name","email","birthday","country","credential","purchased","params"};
     private static final String CREATE_LOCAL_DB = "CREATE TABLE IF NOT EXISTS " + USER_DB_TABLE + 
        "( id INTEGER PRIMARY KEY, name TEXT, email TEXT, birthday TEXT, country TEXT, credential TEXT, purchased TEXT, params TEXT)";


    public User(Context context){
        super(context, LOCAL_DB_NAME, null, LOCAL_DB_VERSION);
        this.load();
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(CREATE_LOCAL_DB);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(User.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + USER_DB_TABLE);
        onCreate(db);
    }

    private String name = "example";
    private String birthday = "10-12-2000";
    private String country = "br";
    private String credential = "xxxxxxxx";
    private String email = "xxxxx@xxxxxx";
    private String purchased = "xxxxxxx";
    private String params = "xxxx";

    // This method saves the this user instance in the local database.
    public void save(){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("id", 0);
        values.put("name", this.name);
        values.put("email", this.email);
        values.put("birthday", this.birthday);
        values.put("country", this.country);
        values.put("credential", this.credential);
        values.put("purchased", this.purchased);
        values.put("params", this.params);

        try{
            db.update(USER_DB_TABLE, values,  "id = ?", new String[] { String.valueOf(0) });
        }catch (Exception e){
            db.insert(USER_DB_TABLE, null, values);
        }

        db.close();

    }

    // This method cleans the local user stored in the database.
    public void clean() {

        this.name = "";
        this.birthday = "";
        this.country = "";
        this.credential = "";
        this.email = "";
        this.purchased = "";
        this.params = "";

        this.save();
    }

    // This method loads the user stored in the database.
    public void load() {
       SQLiteDatabase db = this.getReadableDatabase();
       Cursor cursor = db.query(USER_DB_TABLE, USER_ALL_COLUMNS, "id =?",
                   new String[] { String.valueOf(0) }, null, null, null, null);

       if (cursor.moveToFirst()){
          this.name = cursor.getString(1);
          this.birthday = cursor.getString(2);
          this.country = cursor.getString(3);
          this.credential = cursor.getString(4);
          this.email = cursor.getString(5);
          this.purchased = cursor.getString(6);
          this.params = cursor.getString(7);
       }else{
          this.clean();
       }
       db.close();
    }
}
----------------解决---------------------


我只将第一个id更改为1,效果非常好。谢谢

if (cursor != null)
    cursor.moveToFirst();

this.name = cursor.getString(1);
光标为空。您应该检查moveToFirst的结果,并且只调用get。。。如果返回true:

正在检查光标!=空是不必要的,尽管这是一个好习惯。

尝试添加


cursor.moveToPositionposition

如果cursor.moveToFirst{this.name=cursor.getString1;…}???,此更改将停止异常,但应用程序将继续从数据库中读取任何内容…:你能不能换一种说法,我不确定问题出在哪里。我用这段代码读取值并在textview中打印,使用片段的onCreateView:User User User=new UsergetActivity.getApplicationContext;textView.setTextuser.getName;好的,我相信您需要一个else分支:如果在db中找不到行,请将对象字段(如name)重置为合理的空/默认值。我只将第一个id更改为1,并且工作正常。谢谢
if (cursor.moveToFirst()) {
    this.name = cursor.getString(1);
    ...
} else {
    // reset variables to empty/defaults
}