2014-01-05 3 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()의 결과를 확인하고 true를 돌려주는 경우에만 get...()를 호출해야합니다 : (. 그것은 좋은 습관 비록 cursor != null 점검하는 것이 필요하지 않습니다)

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

커서를 추가 할 수

+0

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

+0

이 변경은 예외를 막지 만 데이터베이스에서 아무 것도 계속 읽지 않습니다. ... ( –

+0

그 내용을 다시 말해 줄 수 있습니까? – laalto

0

시도 .moveToPosition (position);

+0

첫 번째 ID 만 1로 변경했습니다. 그리고 w 완벽하게 ork. 감사. –

관련 문제