[英]Query error to write migration with room database in Android
我正在嘗試在我的項目中遷移我的房間數據庫,並編寫遷移查詢。
在現有表上添加兩個新列。 這是代碼。 這是正確的嗎?
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);
}
}
當我當時嘗試運行時,出現此錯誤。
Migration didn't properly handle: pages.
Expected:
TableInfo{name='pages', columns={help=Column{name='help', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, privacyPolicy=Column{name='privacyPolicy', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, legend=Column{name='legend', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, info=Column{name='info', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, backstory=Column{name='backstory', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, language=Column{name='language', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, termsConditions=Column{name='termsConditions', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, sources=Column{name='sources', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='pages', columns={help=Column{name='help', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, legend=Column{name='legend', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, privacyPolicy=Column{name='privacyPolicy', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, info=Column{name='info', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, language=Column{name='language', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, sources=Column{name='sources', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue=''''}, termsConditions=Column{name='termsConditions', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
SQLite 不支持使用單個語句向表添加多列。 要將多個列添加到表中,您必須執行多個 ALTER TABLE ADD COLUMN 語句。
https://www.sqltutorial.org/sql-add-column/
為每一列添加一個單獨的語句:
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()
:
有關更多詳細信息,請查看此鏈接並閱讀一次。 我知道這很無聊,但必須閱讀。 閱讀詳細信息后,我知道我的錯誤。 :)
這是鏈接 - https://developer.android.com/training/data-storage/room/migrating-db-versions
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.