2014-01-05 138 views
0

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

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 = "[email protected]"; 
    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,并且完美地工作。谢谢。

回答

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

this.name = cursor.getString(1); 

游标是空的。您应该检查的moveToFirst()的结果,只能调用get...()如果返回true:(检查cursor != null是没有必要的,虽然这是一个好习惯)

if (cursor.moveToFirst()) { 
    this.name = cursor.getString(1); 
    ... 
} else { 
    // reset variables to empty/defaults 
} 

+0

如果(cursor.moveToFirst()){ this.name = cursor.getString(1); ... } ???? –

+0

这种变化停止例外,但应用程序继续从数据库中读取任何东西... :( –

+0

你能不能换一种说法,我不知道有什么问题。 – laalto

0

尝试添加

光标.moveToPosition(位置);

+0

我只将第一个id更改为1,并且w奥克斯完美。谢谢。 –