Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 实体类在文件室数据库中不起作用_Kotlin_Entity_Android Room - Fatal编程技术网

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]

您应该为构造函数参数设置默认值。在这种情况下,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(tableName = "geburtstag")
data class Bday(
    @PrimaryKey(autoGenerate = true)
    var id : Int = 0,
    @ColumnInfo(name="Name")
    var name : String = "",
    @ColumnInfo(name="Birthday")
    var birth : String = ""
)