This seems like a lazy question, but the offical docs literally don't explain how to do this.
https://developer.android.com/training/data-storage/room
I have an entity like so:
#Entity
data class Shader(
#PrimaryKey(autoGenerate = true) val uid: Int,
val name: String,
val shaderMainText: String,
val paramsJson: String?
)
And some methods on my Dao:
#Insert
fun insertAll(vararg shaders: Shader)
#Delete
fun delete(shader: Shader)
#Update
fun update(shader: Shader)
How do I make a new record and insert it?
I've gotten so far as this:
val record = Shader(name="Foo", shaderMainText="bar", paramsJson=null)
But it complains that I'm missing the argument uid. Why do I need to provide a uid? It's supposed to be auto-generated?
An example of creating a new record would be appreciated.
You can set 0 for the default value of your uid field
#Entity
data class Shader(
#PrimaryKey(autoGenerate = true) val uid: Int = 0,
val name: String,
val shaderMainText: String,
val paramsJson: String?
)
When autoGenerate is true, Room ignores the default value, so it creates auto-incremented values.
Finally, this doesn't give you a compilation error:
val record = Shader(name="Foo", shaderMainText="bar", paramsJson=null)
The docs say:
If the field type is long or int (or its TypeConverter converts it to a long or int), Insert methods treat 0 as not-set while inserting the item.
If the field's type is Integer or Long (or its TypeConverter converts it to an Integer or a Long), Insert methods treat null as not-set while inserting the item.
So I would try setting the uid to be initialized to 0 or make it nullable and set to null.
Hope that helps!
https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey#autoGenerate()
You Can Also try like this
The best practice is you always create an instance Class
1st Create a Database instance class
#Database(entities = [Shader::class], version = 1)
abstract class DatabaseHelper : RoomDatabase() {
companion object {
private var instance: DatabaseHelper? = null
fun getInstance(context: Context?): DatabaseHelper? {
if (instance == null) {
synchronized(DatabaseHelper::class) {
if (context != null) {
instance = Room.databaseBuilder(context.applicationContext, DatabaseHelper::class.java, "database.db").allowMainThreadQueries().build()
}
}
}
return instance
}
}
abstract fun getDao(): DBDao?
}
Create Your Shader data class like below
#Entity(tableName = "shader")
data class Shader(
#PrimaryKey(autoGenerate = true) var id: Int?,
val name: String,
val shaderMainText: String,
val paramsJson: String?
) {
constructor(name: String, shaderMainText: String, paramsJson: String?) : this(
null,
name,
shaderMainText,
paramsJson
)
}
Here you want to create constructor
Add data in Shader and insertAll Like Below
val record = Shader(name="Foo", shaderMainText="bar", paramsJson=null)
DatabaseHelper.getInstance(this)?.getDao()?.insertAll(record)
And Your Data added is successfully
There should be always a #PrimaryKey(autoGenerate = false) while inserting data in the DB
I am trying to use type converters in Android (Kotlin) so i am using the type converters class but i am getting confused like inside of the clouds i am having a single variable so i have returned it but
#Entity(tableName = "WeatherDb")
data class WeatherDTO(
val base: String,
val clouds: Clouds,
val cod: Int,
val coord: Coord,
val dt: Int,
#PrimaryKey(autoGenerate = true)
val id: Int,
val main: Main,
val name: String,
val sys: Sys,
val timezone: Int,
val visibility: Int,
val weather: List<Weather>,
val wind: Wind
)
class TypeConverters {
#TypeConverter
fun fromCloudsToDouble(clouds: Clouds): Int {
return clouds.all
}
fun fromCoordToDouble(coord: Coord): Double {
}
}
In coord class here are multiple with different datatypes how to covert this?
data class Main(
val feels_like: Double,
val grnd_level: Int,
val humidity: Int,
val pressure: Int,
val sea_level: Int,
val temp: Double,
val temp_max: Double,
val temp_min: Double
)
Clouds.kt
data class Clouds(
val all: Int
)
Coord.kt
data class Coord(
val lat: Double,
val lon: Double
)
Main.kt
data class Main(
val feels_like: Double,
val grnd_level: Int,
val humidity: Int,
val pressure: Int,
val sea_level: Int,
val temp: Double,
val temp_max: Double,
val temp_min: Double
)
Sys.kt
data class Sys(
val country: String,
val id: Int,
val sunrise: Int,
val sunset: Int,
val type: Int
)
Weather.kt
data class Weather(
val description: String,
val icon: String,
val id: Int,
val main: String
)
Wind.kt
data class Wind(
val deg: Int,
val gust: Double,
val speed: Double
)
WeatherViewModel.kt
#HiltViewModel
class WeatherViewModel #Inject constructor(
private val repo:WeatherRepository,
private val application: Application,
private val WeatherDb:WeatherDB,
private val fusedLocationProviderClient: FusedLocationProviderClient
) :ViewModel(){
private val _resp = MutableLiveData<WeatherDTO>()
val weatherResp:LiveData<WeatherDTO>
get() = _resp
private val _cord = MutableLiveData<Coord>()
val cord:LiveData<Coord>
get() = _cord
var locality:String = ""
fun getWeather(latitude:Double,longitude:Double) =
viewModelScope.launch {
repo.getWeather(latitude,longitude).let { response->
if(response.isSuccessful){
Log.d("response","${response.body()}")
WeatherDb.WeatherDao().insertWeather(response.body()!!)
_resp.postValue(response.body())
}else{
Log.d("Weather Error","getWeather Error Response: ${response.message()}")
}
}
}
fun fetchLocation():Boolean{
val task = fusedLocationProviderClient.lastLocation
if(ActivityCompat.checkSelfPermission(application,android.Manifest.permission.ACCESS_FINE_LOCATION)
!=PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(application,android.Manifest.permission.ACCESS_COARSE_LOCATION)
!=PackageManager.PERMISSION_GRANTED
){
return true
}
task.addOnSuccessListener {
if(it!=null){
getWeather(it.latitude,it.longitude)
getAddressName(it.latitude,it.longitude)
Log.d("localityname", locality)
}
}
return true
}
private fun fetchLocationDetails(){
}
private fun getAddressName(lat:Double,long:Double):String{
var addressName = " "
val geoCoder = Geocoder(application, Locale.getDefault())
val address = geoCoder.getFromLocation(lat,long,1)
if (address != null) {
addressName = address[0].adminArea
}
locality = addressName
Log.d("subadmin",addressName.toString())
Log.d("Address", addressName)
return addressName
}
fun getCoordinates(cord:String){
val geocoder = Geocoder(application,Locale.getDefault())
val address = geocoder.getFromLocationName(cord,2)
val result = address?.get(0)
if (result != null) {
getWeather(result.latitude,result.longitude)
getAddressName(result.latitude,result.longitude)
}
}
}
Here is my converter in the Kotlin:
class Converters {
#TypeConverter
fun valueFromDomainToStorage(value: Value): String {
return value.convertToJson()
}
#TypeConverter
fun valueFromStorageToDomain(str: String): Value {
// we can not create an empty instance of value as TypeDecoder.java should call non-empty constructor
return Value(
"just a stub",
BigInteger.valueOf(0),
BigInteger.valueOf(0),
false,
BigInteger.valueOf(0)
)
.fromJson(str)
}
}
where .convertToJson() and .fromJson(str) implemented as extensions within Value class:
fun Value.convertToJson(): String {
val result = JSONObject()
result.put(ValueConst.OFFER_FIELD, offer)
result.put(ValueConst.AVAILABLE_SINCE, availableSince.toLong())
result.put(ValueConst.AVAILABLE_END, availabilityEnd.toLong())
result.put(ValueConst.IS_CONSUMED, isConsumed)
result.put(ValueConst.LOCKED_UNTIL, lockedUntil)
return result.toString()
}
fun Value.fromJson(json: String): Value {
val subj = JSONObject(json)
return Value(
subj.optString(ValueConst.OFFER_FIELD),
BigInteger.valueOf(subj.optLong(ValueConst.AVAILABLE_SINCE)),
BigInteger.valueOf(subj.optLong(ValueConst.AVAILABLE_END)),
subj.optBoolean(ValueConst.IS_CONSUMED),
BigInteger.valueOf(subj.optLong(ValueConst.LOCKED_UNTIL))
)
}
You should implement Converter class for each non-native class type. Do not forget to register your converters on database:
#Database(entities = [ChainTransaction::class], version = 1, exportSchema = false)
#TypeConverters(Converters::class)
abstract class AppDatabase: RoomDatabase() {
When you have compile the code and later introduce new changes, you have to increase version parameter too to make changes to take effect:
#Database(entities = [ChainTransaction::class], version = 2, exportSchema = false)
#TypeConverters(Converters::class)
abstract class AppDatabase: RoomDatabase() {
Here is official documentation and even training on this topic:
https://developer.android.com/training/data-storage/room
so i am using the type converters class but i am getting confused
SQLite (the database around which Room is an object orientated wrapper) is not an object orientated (or aware) database. It is a database that can store primitive types of data which are one of
INTEGER (such as Int or Long), REAL
REAL (such as Float or Double)
TEXT (such as String)
BLOB (such as ByteArray)
NULL
Therefore to store a type of Coord, Cloud or Weather .... you have three options:-
to embed the class, in which case the fields are copied from the embedded class (would be complicated if the embedded classes contained unsupported types). not covered in the answer
to have the class as a table in it's own right with a relationship between it and the parent (WeatherDTO). not covered in the answer
to convert the class to one of the SQLite types (of which either TEXT or BLOB would probably only be practical).
Considering option 3 (TyepConverters) converting the data is of little, if any, use just storing the data as you would not be able to retrieve the data.
As such type converters should always be paired.
One of the pair will be to convert from the class to a type that can be stored.
The other will be to convert from the stored type to the class.
As such you will need quite a few type Converters, that is 2 each for fields:-
clouds (class Clouds)
coord (class Coord)
main (class Main)
sys (class Sys)
weather (class List)
wind (class Wind)
It is the Class of the field that Room looks at to locate the respective type converter.
One of the simplest ways to convert objects (aka classes) is to convert the object to a JSON representation. Although a complexity with this is that there are many JSON libraries and they will often have differences.
For the examples that follow Googles JSON library has been used. However, use of this library with Room doesn't appear to directly support the use of List<the_class> e.g. List.
The dependency for this being (as an example) implementation 'com.google.code.gson:gson:2.10'
As a get around a new class WeatherList has ben used as per:-
data class WeatherList(
val weatherList: List<Weather>
)
and the WeatherDTO class has been changed to use it as per :-
....
//val weather: List<Weather>,
val weather: WeatherList,
....
As such the TypeConverters class could then be:-
class TypeConverters {
#TypeConverter
fun fromCloudsToJSONString(clouds: Clouds): String = Gson().toJson(clouds)
#TypeConverter
fun toCloudsFromJSONString(jsonString: String): Clouds = Gson().fromJson(jsonString,Clouds::class.java)
#TypeConverter
fun fromCoordToJSONString(coord: Coord): String = Gson().toJson(coord)
#TypeConverter
fun toCoordFromJSONString(jsonString: String): Coord = Gson().fromJson(jsonString,Coord::class.java)
#TypeConverter
fun fromMaintoJSONString(main: Main): String = Gson().toJson(main)
#TypeConverter
fun toMainFromJSONString(jsonString: String): Main = Gson().fromJson(jsonString,Main::class.java)
#TypeConverter
fun fromSysToJSONString(sys: Sys): String = Gson().toJson(sys)
#TypeConverter
fun toSysFromJSONString(jsonString: String): Sys = Gson().fromJson(jsonString,Sys::class.java)
#TypeConverter
fun fromWeatherListFromJSONString(weatherList: WeatherList): String = Gson().toJson(weatherList)
#TypeConverter
fun toWeatherListFromJSOnString(jsonString: String): WeatherList = Gson().fromJson(jsonString,WeatherList::class.java)
#TypeConverter
fun fromWindToJSONString(wind: Wind): String = Gson().toJson(wind)
#TypeConverter
fun toWindFromJSONString(jsonString: String): Wind = Gson().fromJson(jsonString,Wind::class.java)
}
As such the all the types/classes/objects that are not directly supported are converted to/from a JSON string representation of the type/class/object.
Note that you need to add the #TypeConverters(#TypeConverters( value = [<????>.TypeConverters::class]). Where has to distinguish between your projects TypeConverters class from Room's (TypeConverters is probably not the best name for the class, renaming it, would overcome the need to distinguish)
Working Example
The following puts the above into action.
As the question does not include the underlying classes, the following have been used:-
data class Coord(
val longitude: Double,
val latitude: Double
)
data class Clouds(
val cover: Double,
val type: String
)
data class Main(
val main: Double
)
data class Sys(
val sys: Double
)
data class WeatherList(
val weatherList: List<Weather>
)
data class Weather(
val weather: Double
)
data class Wind(
val wind: Double
)
The #Dao annotated interface was also made up and is simply:-
#Dao
interface AllDao {
#Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(weatherDTO: WeatherDTO)
#Query("SELECT * FROM weatherdb")
fun getAllFromWeatherDB(): List<WeatherDTO>
}
Also the #Database annotated abstract class was made up it being:-
#TypeConverters( value = [a.a.so74384736typeconverterconfusion.TypeConverters::class])
#Database(entities = [WeatherDTO::class], exportSchema = false, version = 1)
abstract class TheDatabase: RoomDatabase() {
abstract fun getAllDao(): AllDao
companion object {
private var instance: TheDatabase? = null
fun getInstance(context: Context): TheDatabase {
if (instance==null) {
instance = Room.databaseBuilder(context,TheDatabase::class.java,"the_database.db")
.allowMainThreadQueries()
.build()
}
return instance as TheDatabase
}
}
}
Note the package name used to distinguish the TypeConverters class from Room's TypeConverters class
the package name cannot be used elsewhere, so if the above is copied then it would have to be changed. There is no expectation that the code in it's entirety would be copied and used. The code is designed solely to demonstrate the TypeConverters.
Last some activity code to actually do something (store and retrieve some data):-
class MainActivity : AppCompatActivity() {
lateinit var db: TheDatabase
lateinit var dao: AllDao
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
db = TheDatabase.getInstance(this)
dao = db.getAllDao()
dao.insert(
WeatherDTO(
"base001",
Clouds(25.5,"cumulus"),10,
Coord(10.567,30.345),
11,
12,
Main(12345.67890),
"thename",
Sys(9.87654321),
14,
1000,
WeatherList(listOf(Weather(5.1234),Weather(6.5432), Weather(7.6543))),
Wind(23.12)
)
)
for (wdto in dao.getAllFromWeatherDB()) {
Log.d("DBINFO","base = ${wdto.base} longitude = ${wdto.coord.longitude} latitude = ${wdto.coord.latitude} etc ....")
}
}
}
RESULT
When run the log contains, as expected:-
D/DBINFO: base = base001 longitude = 10.567 latitude = 30.345 etc ....
Using App Inspection then the database looks like:-
The fields converted to a JSON string have been highlighted.
Obviously the data will very likely not exactly be as you would expect due to the made up classes.
Follow on from the previous answer #Embedded versus Type Converters
As can be seen from the previous answer, there are some issues in regard to using TypeConverters. From a database perspective the TypeConverters will inevitably contain bloat/unecessary data which is contrary to normalisation (not needlessly storing repetitive data).
As an example the JSON representation will for every row contain exactly the same the field names wasting storage, all rows will have the additional overhead of storing the delimiters ([s and ]s, {s and }s, :s ,s). Furthermore actually using the stored data can become complex due to the bloat and also due to multiple values being stored in a single column and as such can be restrictive.
It would be more efficient to not store the bloat and it could eliminate complexities and enhance the usability of the stored data from a database perspective (querying the data for retrieval) to not store multiple values in a single column.
Using the #Embedded annotation can very easily eliminate the bloat. Consider the following (an alternative version of the WeatherDTO class/entity):-
#Entity(tableName = "WeatherDbAlternative1")
data class WeatherDTOAlternative1(
val base: String,
#Embedded
val clouds: Clouds,
val cod: Int,
#Embedded
val coord: Coord,
val dt: Int,
#PrimaryKey(autoGenerate = true)
val id: Int,
#Embedded
val main: Main,
val name: String,
#Embedded
val sys: Sys,
val timezone: Int,
val visibility: Int,
//val weather: List<Weather>,
/* Unable to embed directly so not embedding */
val weather: WeatherList,
#Embedded
val wind: Wind
)
Bar the weather field all that has been done is add the #Embedded annotation. Noting that the classes of the fields all have fields of types directly supported by Room.
Adding this entity to the #Database annotation and adding a couple of additional functions in the #Dao annotated class as per:-
#Query("SELECT * FROM weatherdbalternative1")
fun getAllFromWeatherDBAlternative1(): List<WeatherDTOAlternative1>
#Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(weatherDTOAlternative1: WeatherDTOAlternative1)
And then amending the Activity code to include :-
/*ALTERNATIVE 1 All but WeatherList embedded */
dao.insert(
WeatherDTOAlternative1(
"base001A",
Clouds(25.5, "cumulus"),
10,
Coord(10.567, 30.345),
11,
12,
Main(12345.67890),
"thenameA1",
Sys(9.87654321),
14,
1000,
WeatherList(listOf(Weather(5.1234), Weather(6.5432), Weather(7.6543))),
Wind(23.12)
)
)
for (wdto in dao.getAllFromWeatherDBAlternative1()) {
Log.d(
"DBINFO",
"base = ${wdto.base} longitude = ${wdto.coord.longitude} latitude = ${wdto.coord.latitude} etc ...."
)
}
Now results in the Log including:-
D/DBINFO: base = base001 longitude = 10.567 latitude = 30.345 etc ....
D/DBINFO: base = base001A longitude = 10.567 latitude = 30.345 etc ....
i.e. effectively the same data is stored and retrievable
However the data is now stored in the database as (ignoring the weather field) as :-
i.e. the data stored is much cleaner but at the expense of additional columns (which can be advantageous).
additionally although not apparent, the fields that have the #Embedded annotation do not need the TypeConverters.
I am getting the following error when I build my application
Not sure how to convert a Cursor to this method's return type (kotlinx.coroutines.flow.Flow<? extends java.util.List<com.notification.NotificationEntity>>)
This is my Entity class
#Entity
internal data class NotificationEntity(
#PrimaryKey #ColumnInfo(name = "notification_id") val notificationId: String,
val title: String,
val body: String?,
#ColumnInfo(name = "is_actionable") val isActionable: Boolean,
#ColumnInfo(name = "created_at") val createdAt: Instant,
#ColumnInfo(name = "is_read") val isRead: Boolean = false)
And this is my Dao Class
#Dao
internal interface NotificationDao {
#Query("SELECT * FROM NotificationEntity ORDER BY created_at ASC")
suspend fun getAllNotifications(): Flow<List<NotificationEntity>>
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveNotification(notifications: List<NotificationEntity>)
}
Can someone help what is the issue here?
When you declare Dao's function which returns Flow the function should not be suspendable. Please see docs.
Change your code to:
#Dao
internal interface NotificationDao {
#Query("SELECT * FROM NotificationEntity ORDER BY created_at ASC")
fun getAllNotifications(): Flow<List<NotificationEntity>>
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveNotification(notifications: List<NotificationEntity>)
}
And add type converter for Instant type which used in NotificationEntity if not yet added.
I am using okhttp to fetch Json data from an API, casting it into Gson for some other purposes, and at the same time, storing of the API data into a Room Database. (It can be storing of the API json data itself, or the casted Gson version) However, I am unsuccessful in this, and hope you could point me to the direction where I am going wrong.
I think i'm missing a typeConverter to convert the data retrieved from API and casted into GSON, to be stored in the Room database. But i'm not sure if i'm correct in this, or if my approach is correct in the first place.
Or could it be an issue with the fact that I have an Entities data class, but gsonData is casted into SampleData data class?
DataGetter
...
...
...
// this suspend function is run within a coroutine.
private suspend fun APICall(url: String, pageNumber: Int, context: Context){
val urlRequest = Request.Builder().url(url).build() // Building of the URL to fetch data
httpClient.newCall(urlRequest).enqueue(object : Callback {
// onFailure -> cutting this out to shorten code
// onResponse is where I am fetching the data and calling put to DB
override fun onResponse(call: Call, response: Response) {
val dataString = response.body!!.string()
val gsonData = gsonResult.fromJson(dataString, SampleData::class.java)
// Do some other stuffs with gsonData separately. This doesn't return anything to gsonData.
putToRoomDb(gsonData, context) // This will eventually be done via another coroutine.
}
}
}
private suspend fun putToRoomDb(sampleData: SampleData, context: Context) {
val db = MyOnlyDatabase.getInstance(context)
db.MyOnlyDao().updateOrInsert(sampleData)
}
Entity
#Entity(tableName = "sample_data_code_names")
data class SampleDataEntities(
#PrimaryKey
var Code: Int,
val Name: String,
val Description: String,
val Item1: String,
val Item2: String
)
Dao
#Dao
interface SampleDataDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateOrInsert(sampleDataEntities: SampleDataEntities)
}
Database
#Database(entities = [SampleDataEntities::class], version = 1, exportSchema = false)
abstract class MyOnlyDatabase: RoomDatabase() {
abstract fun sampleDataDao(): SampleDataDao
companion object {
#Volatile
private var instance: MyOnlyDatabase? = null
fun getInstance(context: Context): MyOnlyDatabase{
return instance ?: synchronized(this) {
instance ?: buildDatabase(context).also { instance = it }
}
}
private fun buildDatabase(context: Context): MyOnlyDatabase{
return Room.databaseBuilder(context, MyOnlyDatabase::class.java, "MyOnlyDatabase.db")
.fallbackToDestructiveMigration()
.build()
}
}
}
Note: Below is the data class for which gsonData is casted into.
data class SampleData(val value: List<SampleDataInfoItems>)
data class SampleDataInfoItems(
val Code: String,
val Name: String,
val Description: String,
val Item1: BigDecimal,
val Item2: BigDecimal
)
You are not annotating your fields according to the JSON response that is coming from the server.
data class SampleDataInfoItems(
#SerializedName("name_this_field_on_json")
val Code: String,
#SerializedName("and_so_on_for_all_fields")
val Name: String,
val Description: String,
val Item1: BigDecimal,
val Item2: BigDecimal
)
Also I think you need the GsonConververFactory for Retrofit.
Please do not mark this as a duplicate I have seen other similar posts but nothing helped
My entity:
#Entity(tableName = "batch_table")
data class Batch(
val batch_id: String? ="",
val batch_name: String? ="",
val user_m_id: String? ="",
val user_profile_id: String? =""
){
#PrimaryKey(autoGenerate = true)
var id1: Int? = 0
constructor():this("","","","")
}
My Dao :
#Dao
interface BatchDao{
#Insert
suspend fun insert(batch : MutableList<Batch>)
#Query("delete from batch_table")
suspend fun deleteBatchTable()
#Query(" select * from batch_table ")
suspend fun getAllBatches() :List<Batch>
#Query("select batch_name from batch_table where batch_id = :batch_id")
suspend fun getBatchName(batch_id:String)
#Transaction
suspend fun insertBatches(batch: MutableList<Batch>){
deleteBatchTable()
insert(batch)
}
I was using room and all I did was add the 4th function (getBatchName) in my dao, but I am not sure why this error started to occur.
The error during compilation:
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - `kotlin.Unit`
I forgot to write the return type of the function but the error that was thrown pointed me to a different thing so I got confused.
#Query("select batch_name from batch_table where batch_id = :batch_id")
suspend fun getBatchName(batch_id:String) : String
.