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

时间:2020-12-11 16:58:14

In my old sqlite table I have these Column (id INTEGER PRIMARY KEY AUTOINCREMENT,api_response_json TEXT, api_req TEXT,post_params TEXT,req_type VARCHAR,timestamp TIMESTAMP)

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

Now I am trying to migrate it to Room DB like this:-

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

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

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");}
};

I am getting an error Migration didn't properly handle

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

Expected: 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 ='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 = []}

Found: 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=[]}

找到: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 = []}

There is problem in TIMESTAMP and VARCHAR .

TIMESTAMP和VARCHAR存在问题。

1 个解决方案

#1


0  

Yes, There is a problem in TIMESTAMP and VARCHAR you should change it to:

是的,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");}
    };

the answer was inside your migration error

答案就在您的迁移错误中

#1


0  

Yes, There is a problem in TIMESTAMP and VARCHAR you should change it to:

是的,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");}
    };

the answer was inside your migration error

答案就在您的迁移错误中