Official website http://greenrobot.org/greendao/
Who is using greenDAO? http://www.appbrain.com/stats/libraries/details/greendao/greendao
Open your root build.gradle add
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
Open your build.gradle (Module: app) add
apply plugin: 'org.greenrobot.greendao' //Must above apply plugin: 'com.android.application'
apply plugin: 'com.android.application'
greendao {
schemaVersion 1
}
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
}
Create class for extends Application
public class App extends Application {
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
MyDevOpenHelper helper = new MyDevOpenHelper(this,"notes-db");
Database db = helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
instance = this;
}
private static App instance;
public static App getInstance() {
return instance;
}
public DaoSession getDaoSession() {
return daoSession;
}
}
Create MyDevOpenHelper class extends DaoMaster.DevOpenHelper for update table
public class MyDevOpenHelper extends DaoMaster.DevOpenHelper{
public MyDevOpenHelper(Context context, String name) {
super(context, name);
}
public MyDevOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
}
}
@Entity
public class Note {
@Id
public Long id;
public String text;
}
DaoSession daoSession = (App.getInstance()).getDaoSession();
NoteDao noteDao = daoSession.getNoteDao();
Note note = new Note();
note.text = "add data";
noteDao.save(note);
DaoSession daoSession = (App.getInstance()).getDaoSession();
NoteDao noteDao = daoSession.getNoteDao();
List<Note> notes = noteDao.loadAll();
int id = 1;//update table id
DaoSession daoSession = (App.getInstance()).getDaoSession();
NoteDao noteDao = daoSession.getNoteDao();
Note note = noteDao.queryBuilder().where(NoteDao.Properties.Id.eq(id)
).unique();
note.text = "update text column";
noteDao.save(note);
int id = 1;//delete table id
DaoSession daoSession = (App.getInstance()).getDaoSession();
NoteDao noteDao = daoSession.getNoteDao();
QueryBuilder<Note> qb = noteDao.queryBuilder();
DeleteQuery<Note> bd = qb.where(NoteDao.Properties.Id.eq(id)).buildDelete();
bd.executeDeleteWithoutDetachingEntities();