我正在尝试将数据插入 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;
}
这是正确的吗?