How to initialize database with certain values activeandroid

343 views Asked by At

I'm using ActiveAndroid as an ORM. I want to initialize my database tables with certain values.Only one time,I dont want the content to be duplicated.

or is there a solution to execute a sql script at installation time.

I searched in the documentation but I didnt found it. Thanks.

1

There are 1 answers

7
Mohammad Rahchamani On BEST ANSWER

I think you can check your table, and if it was empty, then insert your data into it. you can have several tables, and should do the same for each one

List <Category> categories = new Select()
        .from(Category.class)
        .where("your where clause here")    // your where clause
        .execute();

if (categories != null && categories.size() > 0 ) {
      // data is inserted before 
} else {
      Category newCategory = new Category("category");
      newCategory.save();
} 

List <Item> items = new Select()
        .from(Item.class)
        .where("your where clause")
        .execute();

if (items != null && items.size() > 0 ) {
      // data is inserted before 
} else {
      Item item = new Item("item");
      item.save();
} 

now new data will insert into table only when they're not exist in your table and won't duplicate.

and in other solution you might use pre-populated database and use that in your application.