迁移不在房间数据库中工作

时间:2017-11-10 06:51:28

标签: android sqlite database-migration android-room

朋友您好我想在我的应用程序中进行房间数据库迁移,所以下面是我的代码

首先我有员工表并完成了CRUD操作。现在我添加其他表部门,所以我必须执行迁移,旧数据保持不变,所以我为Department类创建类和接口,如下所示

实体类

 @Entity
public class Department implements Serializable {
    @PrimaryKey(autoGenerate = true)
    public long department_id;

    @ForeignKey(entity = Employee.class,
    parentColumns = "employee_id",
    childColumns = "eid", onDelete = CASCADE)
    String department_name;
    String eid;

    public long getDepartment_id() {
        return department_id;
    }

    public void setDepartment_id(long department_id) {
        this.department_id = department_id;
    }

    public String getDepartment_name() {
        return department_name;
    }

    public void setDepartment_name(String department_name) {
        this.department_name = department_name;
    }

    public String getEid() {
        return eid;
    }

    public void setEid(String eid) {
        this.eid = eid;
      }
    }

DAO课程

@Dao 公共接口DepartmentDao {

@Query("SELECT * FROM department")
List<Department> getAll();

@Insert
Long[] insertAll(Department... users);

@Update
int updateDepartment(Department... users);

@Delete
void delete(Department user);

}

Appdatabase.java

@Database(entities = {Employee.class, Department.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase {
public abstract EmployeeDao employeeDao();
public abstract DepartmentDao departmentDao();
}

AppName.java

public class AppName extends Application {


// Room persistence API
AppDatabase db;

@Override
public void onCreate() {
   // ACRA.init(this);
    super.onCreate();

    // Room persistence API
    //this will create and build database and also from here you can control the migration process.
    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "Employee").allowMainThreadQueries().addMigrations(MIGRATION_1_2).build();

}

/**
 * Room persistence API
 * This is migration of database version 1 and 2
 */
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        //here you can do your migration changes of database
       /* database.execSQL("CREATE TABLE `Department` (`department_id` INTEGER, "
                + "`eid` INTEGER, 'department_name' TEXT, PRIMARY KEY(`department_id`))");*/
        database.execSQL(
                "CREATE TABLE Department (department_id INTEGER NOT NULL,"
                        + "department_name,"
                        + "eid INTEGER,"
                        + "PRIMARY KEY(department_id))");
    }

};

/**
 * Room persistence API
 */
public AppDatabase getDatabase() {
    return db;
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
public void onLowMemory() {
    super.onLowMemory();
}

@Override
public void onTerminate() {
    super.onTerminate();
}

@Override
public void onTrimMemory(int p_level) {
    super.onTrimMemory(p_level);
   }
 }

当我运行上面的代码时,我得到如下错误

Caused by: java.lang.IllegalStateException: Migration didn't properly
handle Department(entity.Department). Expected:                       
TableInfo{name='Department',
columns={department_id=Column{name='department_id', type='INTEGER',
notNull=true, primaryKeyPosition=1}, eid=Column{name='eid',
type='TEXT', notNull=false, primaryKeyPosition=0},
department_name=Column{name='department_name', type='TEXT',
notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

Ant想法我该如何解决这个问题?

0 个答案:

没有答案