SQLite:db.update与复合主键?

时间:2015-07-09 10:01:20

标签: sqlite

我对Android编程很陌生,我目前正试图弄清楚如何使用两个表构建一个SQLite数据库。在其中一个表中,我在两列上使用复合主键。我的问题是如何编写更新功能。我想说"更新联系人,其中id1 = x和id2 = y",我该怎么做?

我跟随的例子如下:

public boolean updateContact (Integer id, String name, String phone, String email, String street,String place)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("phone", phone);
        contentValues.put("email", email);
        contentValues.put("street", street);
        contentValues.put("place", place);
        db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
        return true;
    } 

1 个答案:

答案 0 :(得分:2)

db.update("contacts", contentValues, "id = ? and id2 = ?", new String[] { Integer.toString(id), Integer.toString(id2) } );

当然,您需要在某处传入或定义id2。

相关问题