2

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');
Idob
  • 1,620
  • 4
  • 16
  • 27

2 Answers2

3

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".

Idob
  • 1,620
  • 4
  • 16
  • 27
  • It's strange since docs says "this is done using sql-scripts named .sql, where NewVersion is the AA_DB_VERSION". Does it realy work for you? I tried this since I want to achieve exactly the same but id doesn't work in my case :( – Izydorr Jan 12 '15 at 15:38
  • Where is the migration file located? Is it assets/migrations? Try putting breakpoint to DatabaseHelper.executeMigrations() if it finds your upgrade script and executes it. – Idob Jan 13 '15 at 06:11
  • The script is located exactly there. Generally it works if I upgrade the app with new db version. The problem is with starting it on first installation. And I don't know anything about DatabaseHelper class and executeMigrations() function. Are they part of ActiveAndroid? – Izydorr Jan 13 '15 at 11:45
  • Yes, try to search it in your project. I found it in Android Studio by pressing Crtl+Shift+N and writing DatabaseHelper – Idob Jan 13 '15 at 12:03
0

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();