0

I am trying to insert data into the SQLite database and for that I have written a simple method to insert the data that I get from the API as shown below:

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;
    }

I am getting for the last

game.original_game_rating();

which is correctly justified because it is a List of object.

My object class named "GameInfoList" which is passed as the parameter in the method is shown below:

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();

EDIT

I also have GameRatings object as shown below:

@Nullable
public abstract String api_detail_url();

public abstract long id();

@Nullable
public abstract String name();

Now I want to insert the name inside the table for game rating for which I have modified the code as below:

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;
}

Is this right?

3
  • 1
    sqlite database only stores primitive values. you need to store it as text and the while using it you can convert it into List Commented Sep 11, 2017 at 19:01
  • What is your schema for the table you are going to insert this data into? Specifically, what is the data type of the column identified as COLUMN_GAME_ORIGINAL_RELEASE_DATE? Commented Sep 11, 2017 at 19:03
  • @CommonsWare The data type for COLUMN_GAME_ORIGINAL_RELEASE_DATE is TEXT Commented Sep 11, 2017 at 19:23

1 Answer 1

5

You have two choices:

  1. Get rid of that column and have GameRatings go in a separate table, through a 1:N relation with your GameEntry(?) table

  2. Convert your List<GameRatings> into text (e.g., JSON) and store the text in the column

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.