我是Android房间数据库的新手。我看到在这个话题上也有类似的问题,但我真的不明白什么对这个话题有效。因此,在这里,我试图从表中获取第一个日期,但当我使用进行查询时
@Query("select min(date) from user_items where userId = :userId ")
long getFirstDate(long userId);
我得到了结果,因为最短日期是01/01/1970
但我希望表中的第一个日期显示给用户。
我的实体类User.java
:
@Entity(tableName = "users")
public class User {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private String email;
private String password;
private String securityAnswer;
// other staff here
}
我的另一个实体与User
表有关系。
UserItem.java
:
@Entity(tableName = "user_items", indices = {@Index("userId")}, foreignKeys = @ForeignKey(entity = User.class,
parentColumns = "id",
childColumns = "userId",
onDelete = CASCADE))
public class UserItems {
@PrimaryKey(autoGenerate = true)
private int id;
private String category;
private String amount;
private String date;
@ColumnInfo(name = "userId")
private int userId;
// setter and getter
}
除了min(date)
函数外,我的其他所有查询都运行良好。
这是我的实体,我想要我标记的第一个日期,向用户显示
最后我得到了解决方案:这里我更改了我的查询
而不是:
@Query("select min(date) from user_items where userId = :userId ")
long getFirstDate(long userId);
使用此:
@Query("select min(date) from user_items where userId = :userId ")
String getFirstDate(long userId);
因为如果您查看我的UserItem.java
类,这里我将数据存储为String,因此,如果我想将String存储为long
类型,这是不允许的。
希望它能帮助你。
查询返回UNIX epoch(1970年1月1日(的开始,这表明您可能已在其中一行的日期列中持久化(或试图持久化(无效值。您可能需要检查日期列中是否有任何可疑值。