2017-05-08 13 views
0

これは初めてアンドロイドの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

関連する問題