ActiveAndroid Pre populate table using schema migration

非 Y 不嫁゛ 提交于 2019-12-24 02:13:12

问题


I want to create a table for the first time (DB version = 1), and insert into it 2 rows by default. The table needs to be created automatically by ActiveAndroid, and the records should be inserted by the SQL I wrote in 1.sql file.

The table looks fine, but the rows are not inserted at all (no errors thrown).

The model looks like this:

@Table(name = "leagues")
public class League extends Model implements Serializable{

    @Column(name = "name")
    public String name;

    public static List<League> getAll() {
        return new Select()
                .from(League.class)
                .orderBy("name ASC")
                .execute();
    }
}

and the 1.sql:

INSERT INTO leagues (Id, name) VALUES (1, 'Premier league');

回答1:


Apparently that ActiveAndroid thinks the version of the DB is 0 in the initial setup of the app. So, the file "1.sql" has a bigger version than the DB number and it will be skipped!

To make it work, the script name should be "0.sql".




回答2:


If you have initialised ActiveAndroid both with Configuration namely code below and Android Manifest "AA_DB_VERSION"; this is the reason. Remove Configuration.Builder. If you initialise with configuration, ActiveAndroid doesn't get version number from AA_DB_VERSION.

CoConfiguration.Builder(this).setDatabaseName("XXXX.db").create();


来源:https://stackoverflow.com/questions/27153002/activeandroid-pre-populate-table-using-schema-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!