nikmin
nikmin

Reputation: 1843

android ORMLite populate table on database create

I'm using ORMLite in my android application. Is there a way when creating the database in the database helper to add some rows, or I must check in the main activity if there is already a database created and then create it and populate it if it doesn't exists. What I want to achieve: When the application starts for the first time, add some data in the database (when creating the database file).

Upvotes: 1

Views: 1962

Answers (1)

Gray
Gray

Reputation: 116938

You can certainly add as much as you like in the onCreate or onUpgrade methods. Once you create the table, generate a DAO and do dao.create(...);.

If you look at the HelloAndroid example application, you can see that it does exactly that.

public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    TableUtils.createTable(connectionSource, SimpleData.class);
    // here we try inserting data in the on-create as a test
    RuntimeExceptionDao<SimpleData, Integer> dao = getSimpleDataDao();
    // create some entries in the onCreate
    SimpleData simple = new SimpleData(System.currentTimeMillis());
    dao.create(simple);
}

Upvotes: 3

Related Questions