2017-05-08 101 views
0

這是我第一次在android上干預sqlite。Sqlite更新記錄

隨後的教程,創造了SQliteHelper類,還有這個方法裏面:

// Updating single profile 
public int updateProfile(Profile profile) { 

    // 1. get reference to writable DB 
    SQLiteDatabase db = this.getWritableDatabase(); 

    // 2. create ContentValues to add key "column"/value 
    ContentValues values = new ContentValues(); 
    values.put("cpf", profile.getCpf()); // get cpf 
    values.put("nome", profile.getNome()); // get nome 
    values.put("phone", profile.getPhone()); // get nome 

    // 3. updating row 
    int i = db.update(TABLE_PROFILES, //table 
      values, // column/value 
      KEY_ID+" = ?", // selections 
      new String[] { String.valueOf(profile.getId()) }); //selection args 

    // 4. close 
    db.close(); 

    return i; 

} 

但是,我不知道如何使用它..

我知道,爲了得到個人資料我做了以下幾點:

MySQLiteHelper db = new MySQLiteHelper(view.getContext()); 
db.updateProfile(db.getProfile(0)); 

但是我怎麼實際更新它呢?

回答

1

該代碼更新配置文件,在這部分的具體id

// 3. updating row 
int i = db.update(TABLE_PROFILES, //table 
     values, // column/value 
     KEY_ID+" = ?", // selections 
     new String[] { String.valueOf(profile.getId()) }); //selection args 

所以,這id來自你剛纔pased給函數的Profile對象。

你應該這樣做:

// get a profile 
MySQLiteHelper db = new MySQLiteHelper(view.getContext()); 
Profile p = db.getProfile(0); 

// change something 
p.setPhone(55598789); 

// update profile p 
updateProfile(p); 
+0

有道理,謝謝。 – Rosenberg