上一篇 总结了grendao 环境搭建以及简单的增删查改,接下来将全面解析框架的使用,基于上篇的orm模型(Note)数据库讲解
GreenDao的插入:
插入的方式有很多:
daoSession.getNoteDao().insert(note);
//插入note 如果note指定主键与表中已经存在了,就会发生异常(android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: tb_note._id (code 1555))而插入不进去
daoSession.insert(note); 同上
daoSession.getNoteDao().insertOrReplace(note);
当主键存在的时候会替换,所以能够很好的执行插入操作,推荐
daoSession.insertOrReplace(note); 同上
原生的sqlite
String insertSql = String.format("insert into %s (%s,%s,%s) values('%s','%s','%s')",
NoteDao.TABLENAME,
NoteDao.Properties.Title.columnName,
NoteDao.Properties.Content.columnName,
NoteDao.Properties.CreateTime.columnName,
note.getTitle(),
note.getContent(),
note.getCreateTime());
daoSession.getDatabase().execSQL(insertSql);
批量插入
public void insertBatch(Listnotes){
daoSession.getNoteDao().insertInTx(notes);
}
GreenDao的更新操作
1:单条更新(唯一性条件是主键相同)
public voidupdate(Note note) throws Exception {
daoSession.update(note);//daoSession.getNoteDao().update(note);
}
2:批量更新
//批量更新
public void updateBatch(Listnotes) throws Exception {
daoSession.getNoteDao().updateInTx(notes);
}
3:原生sql:
public voidupdateSql(String sql){//daoSession.getDatabase().execSQL("update .....");
daoSession.getDatabase().execSQL(sql);
}
GreenDao 的删除操作
1:单条删除(唯一性是主见相同)
public voiddelete(Note note) throws Exception {
daoSession.delete(note);//daoSession.getNoteDao().delete(note);
}
2: 单条删除 指定主键删除
public void delete(longid) {
daoSession.getNoteDao().deleteByKey(id);
}
3:批量删除
public void deleteBatch(Listnotes) {
daoSession.getNoteDao().deleteInTx(notes);
}
4:批量按主键删除
//批量按主键删除
public void deleteBatchByKey(ListpkIds) {
daoSession.getNoteDao().deleteByKeyInTx(pkIds);
}
5:删除所有
public voiddeleteAll() throws Exception {
daoSession.deleteAll(Note.class);//daoSession.getNoteDao().deleteAll();
}
6:原始sqlite语句
daoSession.getDatabase().execSQL(deletesql);
GreenDao 查询操作 也是最复杂的
1:单条查询
//按主键查询
public Note query(longpk) throws Exception {//daoSession.getNoteDao().load(pk);//按主见查询//daoSession.getNoteDao().load(rowId);//按行号查询
return daoSession.load(Note.class, pk);
}
2: QueryBuilder 复合查询
public ListqueryBuider() throws Exception {//select from tb_note where Content like 'greendao'
return daoSession.getNoteDao().queryBuilder().where(NoteDao.Properties.Content.like("greendao")).build().list();
}
public long queryBuider2(longpk) throws Exception {//select count(*) from tb_note where Content like 'greendao'
return daoSession.getNoteDao().queryBuilder().where(NoteDao.Properties.Content.like("greendao")).buildCount().count();
}
public voidqueryBuider3() throws Exception {
Cursor cursor= null;try{//CursorQuery 部分查询
CursorQuery greendao = daoSession.getNoteDao().queryBuilder().where(NoteDao.Properties.Content.like("greendao")).buildCursor();
cursor=greendao.query();while(cursor.moveToNext()) {long id = cursor.getInt(0);
String title= cursor.getString(1);
String content= cursor.getString(2);
String time=cursor.getString(cursor.getColumnIndex(NoteDao.Properties.CreateTime.columnName));
}
}finally{if (cursor != null)
cursor.close();
}
}
3:查询所有:
public voidqueryAll()
{
daoSession.loadAll(Note.class);
daoSession.queryBuilder(Note.class).build().list();
daoSession.getNoteDao().queryBuilder().build().list();
daoSession.getNoteDao().loadAll();
}