2

我一直是在本机模式下插入初始数据的答案

如何将初始数据添加到 SQLite 数据库?

如何使用 Green dao 获得相同的结果?

这是我的应用程序类

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



    }

}
4

1 回答 1

4

您可以创建一个扩展并在方法DaoMaster内运行查询的类:onCreate

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')");
        }
    }
}

因此,在您的Application课程中,您将使用此类的一个实例:

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();
    }
}
于 2016-02-09T16:55:04.327 回答