如何将现有的sqlite表迁移到具有VCHAR,TIMESTAMP等数据类型的ROOM Db?

sav*_*hra 6 sqlite android database-migration sqldatatypes android-room

在我的旧sqlite表中,我有这些列(id INTEGER PRIMARY KEY AUTOINCREMENT,api_response_json TEXT,api_req TEXT,post_params TEXT,req_type VARCHAR,timestamp TIMESTAMP)

现在我正在尝试将它迁移到Room DB,如下所示: -

static final Migration MIGRATION_1_2 = new migration(1,2){

    @Override
    public void migrate(SupportSQLiteDatabase database) {
    // Create the new table
        database.execSQL(
                "CREATE TABLE users_new (id INTEGER PRIMARY KEY AUTOINCREMENT,api_response_json TEXT, api_req TEXT,post_params TEXT,req_type VARCHAR,timestamp TIMESTAMP)");
    // Copy the data
        database.execSQL("INSERT INTO users_new (id,api_response_json, api_req,post_params,req_type,timestamp) "
                + "SELECT id,api_response_json, api_req,post_params, req_type,timestamp "
                + "FROM old_Table_name");
    // Remove the old table
        database.execSQL("DROP TABLE old_Table_name");
    // Change the table name to the correct one
        database.execSQL("ALTER TABLE users_new RENAME TO old_Table_name");}
};
Run Code Online (Sandbox Code Playgroud)

我收到错误迁移没有正确处理

预期:TableInfo {name ='api_data',columns = {timestamp = Column {name ='timestamp',type ='TEXT',notNull = false,primaryKeyPosition = 0},req_type = Column {name ='req_type',type = 'TEXT',notNull = false,primaryKeyPosition = 0},post_params = Column {name ='post_params',type ='TEXT',notNull = false,primaryKeyPosition = 0},id = Column {name ='id',type = 'INTEGER',notNull = false,primaryKeyPosition = 1},api_response_json = Column {name ='api_response_json',type ='TEXT',notNull = false,primaryKeyPosition = 0},api_req = Column {name ='api_req',type = 'TEXT',notNull = false,primaryKeyPosition = 0}},foreignKeys = [],indices = []}

找到:TableInfo {name ='api_data',columns = {timestamp = Column {name ='timestamp',type ='TIMESTAMP',notNull = false,primaryKeyPosition = 0},req_type = Column {name ='req_type',type = 'VARCHAR',notNull = false,primaryKeyPosition = 0},post_params = Column {name ='post_params',type ='TEXT',notNull = false,primaryKeyPosition = 0},id = Column {name ='id',type = 'INTEGER',notNull = false,primaryKeyPosition = 1},api_response_json = Column {name ='api_response_json',type ='TEXT',notNull = false,primaryKeyPosition = 0},api_req = Column {name ='api_req',type = 'TEXT',notNull = false,primaryKeyPosition = 0}},foreignKeys = [],indices = []}

TIMESTAMP和VARCHAR存在问题.

小智 0

是的,TIMESTAMP 和 VARCHAR 有问题,您应该将其更改为:

    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        // Create the new table
        database.execSQL(
                "CREATE TABLE users_new (id INTEGER PRIMARY KEY AUTOINCREMENT,api_response_json TEXT, api_req TEXT,post_params TEXT,req_type TEXT,timestamp TEXT)");
        // Copy the data
        database.execSQL("INSERT INTO users_new (id,api_response_json, api_req,post_params,req_type,timestamp) "
                + "SELECT id,api_response_json, api_req,post_params, req_type,timestamp "
                + "FROM old_Table_name");
        // Remove the old table
        database.execSQL("DROP TABLE old_Table_name");
        // Change the table name to the correct one
        database.execSQL("ALTER TABLE users_new RENAME TO old_Table_name");}
    };
Run Code Online (Sandbox Code Playgroud)

答案就在您的迁移错误中