0

我正在尝试将数据插入 SQLite 数据库,为此我编写了一个简单的方法来插入从 API 获得的数据,如下所示:

public static ContentValues gameInfoToContentValues(@NonNull GameInfoList game) {

        ContentValues values = new ContentValues();

        values.put(GameEntry.COLUMN_GAME_ID, game.id());
        values.put(GameEntry.COLUMN_GAME_ALIASES, game.aliases());
        values.put(GameEntry.COLUMN_GAME_API_DETAIL_URL, game.api_detail_url());
        values.put(GameEntry.COLUMN_GAME_DATE_ADDED, game.date_added());
        values.put(GameEntry.COLUMN_GAME_DATE_LAST_UPDATED, game.date_last_updated());
        values.put(GameEntry.COLUMN_GAME_SMALL_IMAGE, game.image().small_url());
        values.put(GameEntry.COLUMN_GAME_MEDIUM_IMAGE, game.image().medium_url());
        values.put(GameEntry.COLUMN_GAME_HD_IMAGE, game.image().super_url());
        values.put(GameEntry.COLUMN_GAME_NAME, game.name());
        values.put(GameEntry.COLUMN_GAME_ORIGINAL_RELEASE_DATE, game.original_game_rating());

        return values;
    }

我要最后一次了

game.original_game_rating();

这是正确的,因为它是一个对象列表。

我在方法中作为参数传递的名为“GameInfoList”的对象类如下所示:

public abstract long id();

@Nullable
public abstract String aliases();

@Nullable
public abstract String api_detail_url();

@Nullable
public abstract String date_added();

@Nullable
public abstract String date_last_updated();

@Nullable
public abstract GameImages image();

@Nullable
public abstract String name();

@Nullable
public abstract List<GameRatings> original_game_rating();

编辑

我也有 GameRatings 对象,如下所示:

@Nullable
public abstract String api_detail_url();

public abstract long id();

@Nullable
public abstract String name();

现在我想在我修改了代码的游戏评级表中插入名称,如下所示:

public static ContentValues gameInfoToContentValues(@NonNull GameInfoList game) {

    ContentValues values = new ContentValues();

    if(game.original_game_rating() != null) {
        for(GameRatings ratings : game.original_game_rating()) {
            values.put(GameEntry.COLUMN_GAME_ID, game.id());
            values.put(GameEntry.COLUMN_GAME_ALIASES, game.aliases());
            values.put(GameEntry.COLUMN_GAME_API_DETAIL_URL, game.api_detail_url());
            values.put(GameEntry.COLUMN_GAME_DATE_ADDED, game.date_added());
            values.put(GameEntry.COLUMN_GAME_DATE_LAST_UPDATED, game.date_last_updated());
            values.put(GameEntry.COLUMN_GAME_SMALL_IMAGE, game.image().small_url());
            values.put(GameEntry.COLUMN_GAME_MEDIUM_IMAGE, game.image().medium_url());
            values.put(GameEntry.COLUMN_GAME_HD_IMAGE, game.image().super_url());
            values.put(GameEntry.COLUMN_GAME_NAME, game.name());
            values.put(GameEntry.COLUMN_GAME_ORIGINAL_GAME_RATING, ratings.name());
        }
    }

    return values;
}

这是正确的吗?

4

1 回答 1

5

你有两个选择:

  1. 通过与您的(?) 表的 1:N 关系摆脱该列并GameRatings进入一个单独的表GameEntry

  2. 将您的文本转换List<GameRatings>为文本(例如 JSON)并将文本存储在列中

于 2017-09-11T19:27:02.930 回答