简体   繁体   English

预填充房间数据库会引发错误 - 架构似乎是错误的

[英]Prepopulating room database throws error - Schema seems to be wrong

I try to pre-populate a database in my Android application, but I get the following error:我尝试在我的 Android 应用程序中预填充数据库,但出现以下错误:

 java.lang.IllegalStateException: Pre-packaged database has an invalid schema: Animal(com.example.application.db.Animal).
 Expected:
TableInfo{name='Animal', columns={gender=Column{name='gender', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='Animal', columns={gender=Column{name='gender', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}

I really do not understand what's the problem, it seems to be absolute identical.我真的不明白这是什么问题,它似乎是完全相同的。

This is where in the code the error is thrown, when the db schema is compared to the entries it tries to insert:这是代码中抛出错误的地方,当将 db 模式与它尝试插入的条目进行比较时:

  @Override
  protected RoomOpenHelper.ValidationResult onValidateSchema(SupportSQLiteDatabase _db) {
    final HashMap<String, TableInfo.Column> _columnsAnimal = new HashMap<String, TableInfo.Column>(2);
    _columnsAnimal.put("gender", new TableInfo.Column("gender", "TEXT", false, 0, null, TableInfo.CREATED_FROM_ENTITY));
    _columnsAnimal.put("name", new TableInfo.Column("name", "TEXT", true, 1, null, TableInfo.CREATED_FROM_ENTITY));
    final HashSet<TableInfo.ForeignKey> _foreignKeysAnimal = new HashSet<TableInfo.ForeignKey>(0);
    final HashSet<TableInfo.Index> _indicesAnimal = new HashSet<TableInfo.Index>(0);
    final TableInfo _infoAnimal = new TableInfo("Animal", _columnsAnimal, _foreignKeysAnimal, _indicesAnimal);
    final TableInfo _existingAnimal = TableInfo.read(_db, "Animal");
    if (! _infoAnimal.equals(_existingAnimal)) {  <<<<<<<<<this goes wrong, see next picture
      return new RoomOpenHelper.ValidationResult(false, "Animal(com.johndoes.animalsounds.db.Animal).\n"
              + " Expected:\n" + _infoAnimal + "\n"
              + " Found:\n" + _existingAnimal);
    }

Location where image is thrown抛出图像的位置

This is my entity class:这是我的实体 class:

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "Animal")
public class Animal {

@ColumnInfo(name = "gender")
public final String gender;

@PrimaryKey
@NonNull
@ColumnInfo(name = "name")
public final String name;


public Animal(@NonNull String  name, String gender) {
    this.name = name;
    this.gender = gender;
}
}

And this is the SQL that created the sqlite table that I want to preload:这是创建我要预加载的 sqlite 表的 SQL :

CREATE TABLE "Animal" (
"name"  TEXT,
"gender"    TEXT,
PRIMARY KEY("name")
);

Any ideas would be highly appreciated, thank you.任何想法将不胜感激,谢谢。

as I understood you said your primary key is name but it is absolutely wrong, not even that you defined it as text, the text datatype in database is always nullable you should define a long datatype as your table id and add it as primary key and also remove not null attribute from animal names.据我了解,您说您的主键是名称,但它绝对是错误的,即使您将其定义为文本,数据库中的文本数据类型始终可以为nullable ,您应该定义一个长数据类型作为您的表 ID,并将其添加primary key和还从动物名称中删除 not null 属性。

on your sqlite add "name" TEXT NOT NULL在您的sqlite添加"name" TEXT NOT NULL

hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM