2017-05-08 5 views
0

Dies ist mein erstes Mal Einmischung mit SQLite auf Android.Sqlite Aktualisieren eines Datensatzes

Gefolgt ein Tutorial und erstellt eine SQliteHelper Klasse, diese Methode dort Innere ist:

// 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; 

} 

Aber ich habe keine Ahnung, wie es zu benutzen ..

Ich weiß, dass, um das zu bekommen Profil Ich mache folgendes:

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

Aber wie aktualisiere ich es eigentlich?

Antwort

1

Dieser Code aktualisiert das Profil mit einer spezifischen id in diesem Teil:

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

Also, das id vom Profile Objekt kommt haben Sie nur an die Funktion pased.

Sie sollten etwas tun:

// 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

Sinn macht, danke. – Rosenberg

Verwandte Themen