1

I need to write an app that uses date as id, and I must be able to update just by comparing date

@Entity
data class DailyCount(@PrimaryKey
                  var date:DateTime,
                  var summ: Double = 0.0)

And here is the DAO

@Query("update DailyCount set summ = :sum where date = :today")
fun updateCash(today: DateTime, sum: Double)

I want to make like this :

@Entity
data class DailyCount(@PrimaryKey
                  var date:Date,//JodaTime containing only date
                  var summ: Double = 0.0)

Or like this:

@Query("update DailyCount set summ = :sum where date.today = :dateArg.today")
fun updateCash(dateArg: DateTime, sum: Double)
Arbaz Pirwani
  • 935
  • 7
  • 22
Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78

2 Answers2

3

Room doesn't know how to save your primary field DateTime into database.

You need to write a TypeConverter.

public class TypeConverter {
    private static Gson gson = new Gson();

    @TypeConverter
    public static DateTime stringToDate(String data) {

        Type type = new TypeToken<DateTime>() {
        }.getType();

        return gson.fromJson(data, type);
    }

    @TypeConverter
    public static String dateToString(DateTime dateTime) {
        return gson.toJson(dateTime);
    }
}

Then annotate your primary key

@PrimaryKey
@TypeConverters(TypeConverter::class.java)
var date:DateTime
shb
  • 5,957
  • 2
  • 15
  • 32
0

I was needed to write this query :

@Query("update DailyCount set summ = :sum where date between  :startTimeOfDay and :endTimeOfDay")
fun updateCash(startTimeOfDay: DateTime, endTimeOfDay: DateTime, sum: Double)

I gave here a range with start and end time. This approach returns true when I want to set info in today's Id

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78