簡體   English   中英

SQLite更新記錄

[英]Sqlite updating a record

這是我第一次在Android上使用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));

但是我該如何更新呢?

該代碼在此部分中使用特定的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來自您剛剛粘貼到該函數的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM