android 迁移未正确处理文件室数据库(java.lang.IllegalStateException)

7fyelxc5  于 2023-03-06  发布在  Android
关注(0)|答案(2)|浏览(107)

我遇到致命异常:java.lang.RuntimeException:计算数据库实时数据时出现异常。在崩溃报告中
为什么TableInfo中的所有内容都为空
预期:
表信息{名称="卡片数据",列= {b路径图像=列{名称="b路径图像",类型="文本",关联="2",非空=假,主键位置= 0,默认值="空"},卡片名称=列{名称="卡片名称",类型="文本",关联=" 2 ",非空=假,主键位置= 0,默认值="空"},卡片ID =列{名称="卡片ID",类型="整数",关联="3",非空=真,主键位置= 1,默认值="空"},卡片类型=列{名称="卡片类型",类型="文本",关联=" 2 ",非空=假,主键位置= 0,默认值="空"},时间=列{名称="时间",类型="文本",关联="2",非空=假,主键位置= 0,默认值="空"},fPathImg =列{名称=" fPathImg ",类型="文本",关联=" 2 ",非空=假,主键位置= 0,默认值="null "},sqlDate =列{名称=" sqlDate ",类型=" TEXT ",关联=" 2 ",非Null =假,主键位置= 0,默认值=" null "},卡号=列{名称="卡号",类型="INTEGER",关联="3",非Null =真,主键位置= 0,默认值="null "}},外键=[],索引=[]}
发现:
表信息{名称="卡片数据",列={},外键=[],索引=[]}

    • 这是我的实体类**
@Entity(tableName = "card_data")
@Keep
public class CardDataEntity {

    @PrimaryKey(autoGenerate = true)
    private int cardId;
    private String cardName;
    private String time;
    private String sqlDate;
    private int cardNumber;
    private String fPathImg;
    private String bPathImg;
    private String cardType;
    @Ignore
    private boolean isHeader;

    public CardDataEntity(String cardName, String time, String sqlDate, int cardNumber, String fPathImg, String bPathImg, String cardType) {

        this.cardName = cardName;
        this.time = time;
        this.sqlDate = sqlDate;
        this.cardNumber = cardNumber;
        this.fPathImg = fPathImg;
        this.bPathImg = bPathImg;
        this.cardType = cardType;
        this.isHeader = false;

    }

    // setter getter here

}
    • 这是我的数据库类**
@Database(entities = {ScanDataEntity.class , ScanDataBookmarkEntity.class,
        GenerateDataEntity.class, GenerateBookmarkDataEntity.class, CardDataEntity.class, CardBookmarkDataEntity.class},
        version = 7, exportSchema = false)

public abstract class ScanDatabase extends RoomDatabase {
    public abstract ScanDataDao scanDataDao();
    // DAO classes

    private static ScanDatabase dataBase;

    public static ScanDatabase getInstance(Context context){
        if (null== dataBase){
            dataBase= buildDatabaseInstance(context);
        }
        return dataBase;
    }

    private static final Migration MIGRATION_6_7 = new Migration(6, 7) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            try {
                database.execSQL("ALTER TABLE generate_data "
                        + " ADD COLUMN generateImgPath TEXT");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    };
   
    // same migration for other versions 

    private static ScanDatabase buildDatabaseInstance(Context context) {
        return Room.databaseBuilder(context,
                ScanDatabase.class,
                "scan_database")
                .addMigrations(MIGRATION_6_7,MIGRATION_5_7,MIGRATION_4_7,MIGRATION_3_7,MIGRATION_2_7,MIGRATION_1_7)
//                .fallbackToDestructiveMigration()
                .allowMainThreadQueries().build();

    }

}

有人能帮帮我吗,我真的不知道我做错了什么,或者是一个错误。

n6lpvg4x

n6lpvg4x1#

使用@ColumnInfo注解指定列名以提供与此字段关联的列数据。

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "cardId")
private int cardId;
.....
57hvy0tb

57hvy0tb2#

以同样的错误结束在这里。对于未来也结束在这里的读者:
在我的例子中,我在自生成的Schema文件中有一个简单的打字错误:
架构8.json

"tableName": "measurement",
        "createSql": "CREATE TABLE measurement...

迁移8至12:

database.execSQL("ALTER TABLE measurement RENAME TO measurements;")

架构12.json

"tableName": "measurement",
        "createSql": "CREATE TABLE measurements ...
  • 错误的原因是:tableName应该是measurmentS而不是measurement
  • 希望这对其他人也有帮助,因为错误本身并没有真正帮助我识别这个错别字。*

相关问题