GreenDao 3,安卓移动开发基础案例教程

本文深入探讨了GreenDao 3在Android移动开发中的应用,详细讲解了save与insertOrReplace的区别,如何使用QueryBuilder构建复杂查询,Property在设置查询条件中的作用,分页查询的实现,以及事务处理的多种方式。此外,还涵盖了自定义SQL语句的拼接和执行,以及数据的删除操作。
摘要由CSDN通过智能技术生成

}

}

db.setTransactionSuccessful();

} finally {

db.endTransaction();

}

}

save 和 insertOrReplace 区别

save it will be inserted (key is null) or updated (key is not null)

有key的对象执行更新,无key的执行插入

当对象有key但并不在数据库时会执行失败.适用于保存本地列表

/**

  • “Saves” an entity to the database: depending on the existence of the key property, it will be inserted

  • (key is null) or updated (key is not null).

  • This is similar to {@link #insertOrReplace(Object)}, but may be more efficient, because if a key is present,

  • it does not have to query if that key already exists.

*/

public void save(T entity) {

if (hasKey(entity)) {

update(entity);

} else {

insert(entity);

}

}

insertOrReplace

传入的对象在数据库中,有则更新无则插入,源码显示带有事物和线程

推荐同步数据库时使用该方法

/**

  • Insert an entity into the table associated with a concrete DAO.

  • @return row ID of newly inserted entity

*/

public long insertOrReplace(T entity) {

return executeInsert(entity, statements.getInsertOrReplaceStatement(), true);

}

private long executeInsert(

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

开源分享完整内容戳这里

T entity, DatabaseStatement stmt, boolean setKeyAndAttach) {

long rowId;

if (db.isDbLockedByCurrentThread()) {

rowId = insertInsideTx(entity, stmt);

} else {

// Do TX to acquire a connection before locking the stmt to avoid deadlocks

db.beginTransaction();

try {

rowId = insertInsideTx(entity, stmt);

db.setTransactionSuccessful();

} finally {

db.endTransaction();

}

}

if (setKeyAndAttach) {

updateKeyAfterInsertAndAttach(entity, rowId, true);

}

return rowId;

}

QueryBuilder 构建查询语

支持函数查询条件 distinct、 where 、whereOr 、or 、and、 OrderAsc/Desc、 join、 limit/offset(分页两个合用) 等具体查看QueryBuilder API

构建对象 CursorQuery buildCursor()、List list()、 T uniq

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值