Android SQLite复合主键。我还应该实现BaseColumns吗?

时间:2015-04-06 19:15:42

标签: android sqlite

如果我想在Android中的两列SQLite表中实现多对多关系,请执行以下操作:

CREATE TABLE favorites (
  user_key, 
  bar_key, 
  FOREIGN KEY (user_key) references User(user_id),
  FOREIGN KEY (bar_key) references Bar(bar_id), 
  PRIMARY KEY (column1, column2)
);

我的Contract类中的Table类是否仍然实现BaseColumns?在我看来,这将是这个表的不必要的主键

public static final class FavoritesEntry implements BaseColumns {
    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_FAVORITES).build();

    public static final String CONTENT_TYPE =
            "vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_FAVORITES;
    public static final String CONTENT_ITEM_TYPE =
            "vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_FAVORITES;

    public static final String TABLE_NAME = "favorites";

    public static final String COLUMN_USER_KEY = "user_key";
    public static final String COLUMN_BAR_KEY = "bar_key";

    public static Uri buildFavoritesUri(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }
}

0 个答案:

没有答案