In the Android room, a permanent library is how to insert the entire Model object into a table that has a different list.
Let me show you what I mean:
@Entity(tableName = TABLE_NAME) public class CountryModel { public static final String TABLE_NAME = "Countries"; @PrimaryKey private int idCountry; private List<CountryLang> countryLang = null; public int getIdCountry() { return idCountry; } public void setIdCountry(int idCountry) { this.idCountry = idCountry; } public String getIsoCode() { return isoCode; } public void setIsoCode(String isoCode) { this.isoCode = isoCode; } public List<CountryLang> getCountryLang() { return countryLang; } public void setCountryLang(List<CountryLang> countryLang) { this.countryLang = countryLang; } }
my DAO looks like this:
@Dao public interface CountriesDao{ @Query("SELECT * FROM " + CountryModel.TABLE_NAME +" WHERE isoCode =:iso_code LIMIT 1") LiveData<List<CountryModel>> getCountry(String iso_code); @Query("SELECT * FROM " + CountryModel.TABLE_NAME ) LiveData<List<CountryModel>> getAllCountriesInfo(); @Insert(onConflict = REPLACE) Long[] addCountries(List<CountryModel> countryModel); @Delete void deleteCountry(CountryModel... countryModel); @Update(onConflict = REPLACE) void updateEvent(CountryModel... countryModel); }
When I call database.CountriesDao().addCountries(countryModel); I get the following database compilation error: Error: (58, 31) error: I canโt figure out how to save this field in the database. You might consider adding a type converter for it.
should there be another table named CountryLang? and if so, how do I specify the number to link them in the insert statement?
The CountryLang object itself looks like this:
public class CountryLang { private int idCountry; private int idLang; private String name; public int getIdCountry() { return idCountry; } public void setIdCountry(int idCountry) { this.idCountry = idCountry; } public int getIdLang() { return idLang; } public void setIdLang(int idLang) { this.idLang = idLang; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
The answer looks like this:
"country_lang": [ { "id_country": 2, "id_lang": 1, "name": "Austria" } ]
For each country, therefore, there will not be more than one point. Itโs more convenient for me to call it only one element in the country_lang list. So I can just make a table for country_lang and then somehow link it to CountryModel. but how? can i use a foreign key? I was hoping that I did not need to use a flat file. so you say i have to store it as json? It is recommended not to use the room for temporary? what to use instead?
android android room
j2emanue
source share