Kotlin 实体类在文件室数据库中不起作用
出现以下错误:Kotlin 实体类在文件室数据库中不起作用,kotlin,entity,android-room,Kotlin,Entity,Android Room,出现以下错误: 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). - andr
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
)
问题似乎是您没有在用@Database注释的类中的实体中定义Bday类 e、 g.您似乎有:-
@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)
编译成功
- 注意从
到实体=[]
实体=[Bday::class]
@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(tableName = "geburtstag")
data class Bday(
@PrimaryKey(autoGenerate = true)
var id : Int = 0,
@ColumnInfo(name="Name")
var name : String = "",
@ColumnInfo(name="Birthday")
var birth : String = ""
)