Android Room defaultValue =“ CURRENT_TIMESTAMP”不起作用

时间:2019-12-15 01:00:39

标签: android sqlite android-room

我正在尝试将Room 2.2随附的架构默认值实现到我的实体,但是它似乎不起作用。

我有一个名为Place的实体:

        Form{
        Section(header: Text("Action".uppercased())) {
            HStack {
                Button(action: {}){Image("image").foregroundColor(self.canEat ? .green : .red)}.onTapGesture {
                    self.canEat.toggle()
                }
                Spacer()
                Button(action: {}){Image("image").foregroundColor(self.canDrink ? .green : .red)}.onTapGesture {
                    self.canDrink.toggle()
                }
                Spacer()
                Button(action: {}){Image("image").foregroundColor(self.canSleep ? .green : .red)}.onTapGesture {
                    self.canSleep.toggle()
                }
                Spacer()
                Button(action: {}){Image("image").foregroundColor(self.canDance ? .green : .red)}.onTapGesture {
                    self.canDance.toggle()
                }
                Spacer()
                Button(action: {}){Image("image").foregroundColor(self.canEntertain ? .green : .red)}.onTapGesture {
                    self.canEntertain.toggle()
                }
                Spacer()
            }
        }
}


将位置插入数据库后,@Entity public class Place implements Parcelable { @PrimaryKey(autoGenerate = true) private Long placeId; private String name; private double latitude; private double longitude; private int radius; @ColumnInfo(defaultValue = "CURRENT_TIMESTAMP") private String createdAt; @Ignore public Place() {} public Place(String name, double latitude, double longitude, int radius) { this.name = name; this.latitude = latitude; this.longitude = longitude; this.radius = radius; } // getters and setters... public String getCreatedAt() { return createdAt; } public void setCreatedAt(String createdAt) { this.createdAt = createdAt; } } 字段始终以NULL插入。

enter image description here


createdAt变量更改为createdAt会将字段替换为0。

我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

您可以对特定的列名(和值)使用 @Query ,但不包括默认值。

按照:-

  

请注意,如果仅使用@Insert插入Entity,则不会使用此处指定的默认值。在这种情况下,任何值   将使用Java / Kotlin中指定的值。结合使用@Query和INSERT   语句并在那里跳过此列以使用此默认值   值。 defaultValue

因此,在这种情况下,您可以使用:-

@Query("INSERT INTO Place (name,latitude,longitude,radius) VALUES(:name,:latitude,:logitude,:radius)")

原因是,除了rowid的别名。如果在显式或隐式指定列名的地方传递了null,则不使用默认值,因此值存储为null。

考虑:-

DROP TABLE IF EXISTS place;
CREATE TABLE IF NOT EXISTS `Place` (`placeId` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT, `latitude` REAL NOT NULL, `longitude` REAL NOT NULL, `radius` INTEGER NOT NULL, `createdAt` TEXT DEFAULT CURRENT_TIMESTAMP);

INSERT INTO place VALUES(null,'opt1',0,0,0,null); //<<<< As per @Insert
INSERT INTO place (name,latitude,longitude,radius) VALUES('opt2',1,1,1); //<<<< As per @Query
SELECT * FROM place;

结果:-

enter image description here

Room实际上会构建以下2个查询(根据生成的代码)

"INSERT OR ABORT INTO `Place` (`placeId`,`name`,`latitude`,`longitude`,`radius`,`createdAt`) VALUES (?,?,?,?,?,?)"

"INSERT INTO place (name,latitude,longitude,radius) VALUES(?, ?, ?, ?)"