Android Room:未生成通过@DatabaseView标记的视图

时间:2018-12-22 08:13:26

标签: android android-room androidx

这是我的代码。我已经按照说明完成了所有工作。通过@DATABASEVIEW(value = ?, viewName =?)创建的视图标记 在Room数据库类中标记了视图 但是当我在构建过程中针对VIEWS查询选择进行查询时: 查询存在问题:[SQLITE_ERROR] SQL错误或数据库丢失(无此表:USER_RATE_INFO_VIEW 我探索了自动生成的数据库实现类,但没有找到视图创建脚本。 enter image description here

@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;

}

1 个答案:

答案 0 :(得分:0)

我有相同的错误,只需解决即可。 (我使用kotlin,因此您需要在java中进行相应的更改)

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

  

项目根目录/ 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)
    }
}
  

app / build.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

我花了几个小时来调试和解决此错误,希望它能为您提供帮助。

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

相关问题