简体   繁体   English

SQLite更新记录

[英]Sqlite updating a record

This is my first time meddling with sqlite on android. 这是我第一次在Android上使用sqlite。

Followed a tutorial and created a SQliteHelper Class, there is this method inside: 遵循了一个教程并创建了一个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;

}

However, I have no idea how to use it.. 但是,我不知道如何使用它。

I know that in order to get the profile I do the following: 我知道为了获取个人资料,请执行以下操作:

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

But how do I actually update it? 但是我该如何更新呢?

That code updates the profile with an specific id in this part: 该代码在此部分中使用特定的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

So, this id comes from the Profile object you have just pased to the function. 因此,此id来自您刚刚粘贴到该函数的Profile对象。

You should do something like: 您应该执行以下操作:

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