如何在 android 中使用 GreenDao 为数据库添加初始数据?
How to add initial data for DB using GreenDao in android?
我一直在本机模式下插入初始数据
How to add initial data to SQLite database?
如何使用 Green dao 获得相同的结果?
这是我的申请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();
}
}
您可以创建一个 class 来扩展 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
class 中,您将使用此 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();
}
}
我一直在本机模式下插入初始数据
How to add initial data to SQLite database?
如何使用 Green dao 获得相同的结果?
这是我的申请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();
}
}
您可以创建一个 class 来扩展 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
class 中,您将使用此 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();
}
}