Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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
在Android中使用room数据库写入迁移时出现查询错误_Android_Database_Sqlite_Android Room - Fatal编程技术网

在Android中使用room数据库写入迁移时出现查询错误

在Android中使用room数据库写入迁移时出现查询错误,android,database,sqlite,android-room,Android,Database,Sqlite,Android Room,我试图在我的项目上迁移我的房间数据库,并为迁移编写查询 在现有表上添加两个新列。这是代码是这样吗? private static final Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE pages ADD CO

我试图在我的项目上迁移我的房间数据库,并为迁移编写查询

在现有表上添加两个新列。这是代码是这样吗?

private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE pages ADD COLUMN sources TEXT NOT NULL default '')");
    }
};
以下是AppDatabase代码:

@Database(entities = {DayInfoEntity.class, UserEntryEntity.class, CycleInfoEntity.class, CycleInfoTempEntity.class, PagesEntity.class}, version = 2, exportSchema = true)
@TypeConverters({DateConverter.class})
public abstract class AppDatabase extends RoomDatabase {

    private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE pages ADD COLUMN sources TEXT NOT NULL default ''");
        }
    };

    private static AppDatabase appDatabase;

    @VisibleForTesting
    private static final String DATABASE_NAME = "app_name";

    public abstract PagesDao pagesDao();

    private final MutableLiveData<Boolean> mIsDatabaseCreated = new MutableLiveData<>();

    public static AppDatabase getInstance(final Context context) {
        if (appDatabase == null) {
            synchronized (AppDatabase.class) {
                if (appDatabase == null) {
                    appDatabase = buildDatabase(context.getApplicationContext());
   appDatabase.updateDatabaseCreated(context.getApplicationContext());
                }
            }
        }
        return appDatabase;
    }

    private static AppDatabase buildDatabase(final Context appContext) {
        return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME)
                .allowMainThreadQueries()
                .addMigrations(MIGRATION_1_2)
                .build();
    }

    private void updateDatabaseCreated(final Context context) {
        if (context.getDatabasePath(DATABASE_NAME).exists()) {
            setDatabaseCreated();
        }
    }

    private void setDatabaseCreated() {
        mIsDatabaseCreated.postValue(true);
    }
}
SQLite不支持使用 单一声明。要向表中添加多列,必须执行 多个ALTERTABLE ADD列语句

为每列添加一条单独的语句:

  database.execSQL("ALTER TABLE pages ADD COLUMN sources TEXT NOT NULL default '' ");
  database.execSQL("ALTER TABLE pages ADD COLUMN backstory TEXT NOT NULL default '' ");

我通过修改下面的代码来解决这个问题。不要忘记在数据库文件中添加此
fallbackToDestructiveMigration()

有关更多详细信息,请查看此链接并阅读一次。我知道那很无聊,但我必须读一读。读了详细资料后,我知道了我的错误。:)

以下是链接-


从您编写的查询中删除
。它应该可以正常工作。请再次检查…你能给我举个例子吗。因为找不到链接请检查我更新的问题,因为你告诉我,我以前一个接一个地尝试过,我收到了这个错误。你更新实体类了吗?您还必须在那里添加新列@ColumnInfo(name=“sources”)@非空私有字符串源;这将删除表中的旧数据。
  database.execSQL("ALTER TABLE pages ADD COLUMN sources TEXT NOT NULL default '' ");
  database.execSQL("ALTER TABLE pages ADD COLUMN backstory TEXT NOT NULL default '' ");
 private static AppDatabase buildDatabase(final Context appContext) {
    return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME)
            .allowMainThreadQueries()
            .fallbackToDestructiveMigration() // ADD THIS WHILE YOU DOIGN MIGRATION
            .addMigrations(MIGRATION_1_2)
            .build();
}