2

I have been this answer for insert initial data in native mode

How to add initial data to SQLite database?

How can I get the same result using Green dao?

this is my Application class

public class App extends Application{


    @Override
    public void onCreate() {

        super.onCreate();
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"Images-bd",null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();



    }

}
Community
  • 1
  • 1
angel
  • 4,474
  • 12
  • 57
  • 89

1 Answers1

4

You can create a class that extends DaoMaster and run your queries inside onCreate method:

public class CustomDaoMaster extends DaoMaster {
    public CustomDaoMaster(SQLiteDatabase db) {
        super(db);
    }

    public static class OpenHelper extends DaoMaster.OpenHelper {
        public OpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
            super(context, name, factory);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            super.onCreate(db);
            db.execSQL("INSERT INTO myTable VALUES('foo')");
            db.execSQL("INSERT INTO myTable VALUES('bar')");
        }
    }
}

So, in your Application class, you will use a instance of this class:

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        CustomDaoMaster.OpenHelper helper = new CustomDaoMaster.OpenHelper(this, "Images-bd", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        CustomDaoMaster daoMaster = new CustomDaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
    }
}
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
  • I solved of other way, extending of openhelper, and using GreenDao inside it for insert records in onCreate. – angel Feb 09 '16 at 18:09
  • Be careful with this example! This is right (you can override onUpgrade as well) but it can be harmful, because the DevOpenHelper will drop and recreate ALL TABLES on every version upgrade! Otherwise use ...OpenHelper (without Dev-Prefix) – r00tandy Sep 06 '16 at 08:50