Sqlite更新记录
问题描述:
这是我第一次在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));
但是我怎么实际更新它呢?
答
该代码更新配置文件,在这部分的具体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);
有道理,谢谢。 – Rosenberg