首页 > 解决方案 > Android Room:未生成通过 @DatabaseView 标签的视图

问题描述

这是我的代码。我已经按照说明做了所有事情。通过 @DATABASEVIEW(value=?, viewName=?) 创建视图标记在 Room 数据库类中标记视图但是当我针对 VIEWS 查询选择时,在构建过程中抛出异常: 查询有问题:[SQLITE_ERROR] SQL 错误或丢失数据库(没有这样的表:USER_RATE_INFO_VIEW 我探索了自动生成的数据库实现类,但没有找到视图创建脚本。 在此处输入图像描述

@Database(entities = {BankEntity.class, BankRateEntity.class, CommentEntity.class, ExchangeOfficeEntity.class,
        FBankEntity.class, FBankRateEntity.class, FRateEntity.class, RateByUserEntity.class,
        RateInfoEntity.class, UserEntity.class, UserMessageEntity.class, UserRateEntity.class},
        views = {BankRateView.class, ChatView.class, FBankRateView.class, RateInfoView.class, UserRateVew.class},
        version = 9)
public abstract class RoomDB extends RoomDatabase {
    private static RoomDB INSTANCE;

    public abstract RoomDao roomDao();

    public static RoomDB getDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), RoomDB.class, "database.db")
                            // allow queries on the main thread.
                            // Don't do this on a real app! See PersistenceBasicSample for an example.
                            .allowMainThreadQueries()
                            .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9)
                            .build();
        }
        return INSTANCE;
    }
}

@Getter
@Setter
@DatabaseView(value = TUserRate.VIEW_SQL_CMD, viewName = TUserRate.VIEW_NAME)
public class UserRateVew extends UserRateEntity{

    @ColumnInfo(name = TUserRate.COLUMN_USER_RATING)
    private float userRating;

}

标签: androidandroid-roomandroidx

解决方案


我有同样的错误,只是解决它。(我用的是kotlin,所以你需要在java中相应地改变一些东西)

1.检查你的build.gradle依赖(有两个,一个显示在app文件夹的“Android”中,另一个显示在项目根文件夹的“Project”中)

项目 root / build.gradle 我添加如下

ext.ktlintVersion = '0.30.0' in buildscript{}
plugins {
    id "com.diffplug.gradle.spotless" version "3.13.0"
}
spotless {
kotlin {
    target "**/*.kt"
    ktlint(ktlintVersion)
    }
}

应用程序/构建.gradle

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
} in android{}

following dependency seems must at top
kapt "androidx.room:room-compiler:2.1.0-beta01"
implementation 'androidx.core:core-ktx:1.0.2'

this one just below previous mention denpendency
implementation 'androidx.room:room-runtime:2.1.0-beta01'
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0-alpha01"

following at bottom but before androidTestImplementation; kotlin_version should be 1.3.31
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1"

2.检查使用@databaseview的DAO实现,特别是必须匹配查询视图结果列的返回类型

当我更改我的 build.gradle 时,错误变成了导致数据库视图不生成的实际错误。

3.使缓存无效/重启android studio

我花了几个小时来调试和解决这个错误,希望它可以帮助你。

(我不是来自英语国家,所以请享受我的英语语法错误。哈哈哈)


推荐阅读