实体 class 不在房间数据库中工作
Entity class do not work in room database
出现以下错误:
Entity class must be annotated with @Entity - androidx.room.Entityerror: 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). - androidx.room.Entityerror: An entity must have at least 1 field annotated with @PrimaryKey - androidx.room.Entityerror: [SQLITE_ERROR] SQL error or missing database (near ")": syntax error) - androidx.room.EntityH:\Apps2021\app\build\tmp\kapt3\stubs\debug\de\tetzisoft\danza\data\DAO.java:17: error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: geburtstag)
实体看起来像这样
@Entity(tableName = "geburtstag")
data class Bday(
@PrimaryKey(autoGenerate = true)
var id : Int,
@ColumnInfo(name="Name")
var name : String,
@ColumnInfo(name="Birthday")
var birth : String
)
问题似乎是您没有在 class 中用 @Database 注释的实体中定义生日 class。
例如你似乎有:-
@Database(entities = [],version = 1)
abstract class TheDatabase: RoomDatabase() {
abstract fun getDao(): Dao
}
这会产生您显示的结果,例如
E:\AndroidStudioApps\SO67560510Geburtstag\app\build\tmp\kapt3\stubs\debug\a\a\so67560510geburtstag\TheDatabase.java:7: error: @Database annotation must specify list of entities
public abstract class TheDatabase extends androidx.room.RoomDatabase {
^error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: geburtstag) - a.a.so67560510geburtstag.Dao.getAll()error: Not sure how to convert a Cursor to this method's return type (java.util.List<a.a.so67560510geburtstag.Bday>). - a.a.so67560510geburtstag.Dao.getAll()error: a.a.so67560510geburtstag.Dao is part of a.a.so67560510geburtstag.TheDatabase but this entity is not in the database. Maybe you forgot to add a.a.so67560510geburtstag.Bday to the entities section of the @Database? - a.a.so67560510geburtstag.Dao.insert(a.a.so67560510geburtstag.Bday)
同时 :-
@Database(entities = [Bday::class],version = 1)
abstract class TheDatabase: RoomDatabase() {
abstract fun getDao(): Dao
}
编译成功。
- 注意从
entities=[]
到 entities = [Bday::class]
的变化
您应该为构造函数参数设置默认值。在这种情况下,kotlin 将不生成 arg 构造函数。试试这个
@Entity(tableName = "geburtstag")
data class Bday(
@PrimaryKey(autoGenerate = true)
var id : Int = 0,
@ColumnInfo(name="Name")
var name : String = "",
@ColumnInfo(name="Birthday")
var birth : String = ""
)
出现以下错误:
Entity class must be annotated with @Entity - androidx.room.Entityerror: 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). - androidx.room.Entityerror: An entity must have at least 1 field annotated with @PrimaryKey - androidx.room.Entityerror: [SQLITE_ERROR] SQL error or missing database (near ")": syntax error) - androidx.room.EntityH:\Apps2021\app\build\tmp\kapt3\stubs\debug\de\tetzisoft\danza\data\DAO.java:17: error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: geburtstag)
实体看起来像这样
@Entity(tableName = "geburtstag")
data class Bday(
@PrimaryKey(autoGenerate = true)
var id : Int,
@ColumnInfo(name="Name")
var name : String,
@ColumnInfo(name="Birthday")
var birth : String
)
问题似乎是您没有在 class 中用 @Database 注释的实体中定义生日 class。
例如你似乎有:-
@Database(entities = [],version = 1)
abstract class TheDatabase: RoomDatabase() {
abstract fun getDao(): Dao
}
这会产生您显示的结果,例如
E:\AndroidStudioApps\SO67560510Geburtstag\app\build\tmp\kapt3\stubs\debug\a\a\so67560510geburtstag\TheDatabase.java:7: error: @Database annotation must specify list of entities
public abstract class TheDatabase extends androidx.room.RoomDatabase {
^error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: geburtstag) - a.a.so67560510geburtstag.Dao.getAll()error: Not sure how to convert a Cursor to this method's return type (java.util.List<a.a.so67560510geburtstag.Bday>). - a.a.so67560510geburtstag.Dao.getAll()error: a.a.so67560510geburtstag.Dao is part of a.a.so67560510geburtstag.TheDatabase but this entity is not in the database. Maybe you forgot to add a.a.so67560510geburtstag.Bday to the entities section of the @Database? - a.a.so67560510geburtstag.Dao.insert(a.a.so67560510geburtstag.Bday)
同时 :-
@Database(entities = [Bday::class],version = 1)
abstract class TheDatabase: RoomDatabase() {
abstract fun getDao(): Dao
}
编译成功。
- 注意从
entities=[]
到entities = [Bday::class]
的变化
您应该为构造函数参数设置默认值。在这种情况下,kotlin 将不生成 arg 构造函数。试试这个
@Entity(tableName = "geburtstag")
data class Bday(
@PrimaryKey(autoGenerate = true)
var id : Int = 0,
@ColumnInfo(name="Name")
var name : String = "",
@ColumnInfo(name="Birthday")
var birth : String = ""
)