}
}
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