(1)插入
- 常用API
//这是最简单的插入语句,新增一行数据,返回值为行号
public long insert(T entity)
//传递一个数组,新增多行数据
public void insertInTx(T... entities)
//传递一个集合,新增多行数据
public void insertInTx(Iterable<T> entities)
//传递一个集合,新增多行数据,setPrimaryKey:是否设置主键
public void insertInTx(Iterable<T> entities, boolean setPrimaryKey)
//将给定的实体插入数据库,若此实体类存在,则覆盖
public long insertOrReplace(T entity)
//使用事务操作,将给定的实体插入数据库,若此实体类存在,则覆盖
public void insertOrReplaceInTx(T... entities)
//使用事务操作,将给定的实体插入数据库,若此实体类存在,则覆盖
public void insertOrReplaceInTx(Iterable<T> entities)
//使用事务操作,将给定的实体插入数据库,若此实体类存在,则覆盖,并设置是否设定主键
public void insertOrReplaceInTx(Iterable<T> entities, boolean setPrimaryKey)
- 插入
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "demo.db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
StudentDao studentDao = daoSession.getStudentDao();
for (int i = 0; i < 10; i++) {
Student student = new Student();
student.setMId(i+"");
student.setHobby("篮球");
student.setMAge(i);
student.setClassName("一年级");
long id = studentDao.insertOrReplace(student);
Log.e(TAG, "onCreate: "+id );
}
![image.png](https://upload-images.jianshu.io/upload_images/13436492-1863a32f46791b20.png&originHeight=820&originWidth=1534&size=91562&status=done&style=none&width=767?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
image.png
<a name="g8Bsn"></a>
(2)查询
- Dao查询常用方法
//根据主键来查询一条数据
public T load(K key)
//根据行号来查询一条数据,行号从1开始
public T loadByRowId(long rowId)
//查询表中所有的数据
public List<T> loadAll()
- Dao查询
Student load = studentDao.load("1");
Log.e(TAG, "onCreate: "+load.toString() );
- QueryBuilder查询常用方法
// 条件,AND 连接
public QueryBuilder<T> where(WhereCondition cond, WhereCondition... condMore)
// 条件,OR 连接
public QueryBuilder<T> whereOr(WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore)
//去重
public QueryBuilder<T> distinct()
//分页
public QueryBuilder<T> limit(int limit)
//偏移结果起始位,配合limit(int)使用
public QueryBuilder<T> offset(int offset)
//排序,升序
public QueryBuilder<T> orderAsc(Property... properties)
//排序,降序
public QueryBuilder<T> orderDesc(Property... properties)
// 排序,自定义
public QueryBuilder<T> orderCustom(Property property, String customOrderForProperty)
// 排序,SQL 语句
public QueryBuilder<T> orderRaw(String rawOrder)
//本地化字符串排序,用于加密数据库无效
public QueryBuilder<T> preferLocalizedStringOrder()
//自定义字符串排序,默认不区分大小写
public QueryBuilder<T> stringOrderCollation(String stringOrderCollation)
- 返回结果的方法
//值返回一条结果,如果返回结果的数量>1,则报错。如果返回结果的数量等于0,那么返回null。
public T unique()
//值返回一条结果,如果返回结果的数量>1或=0,则报错。
public T uniqueOrThrow()
//返回一个集合
public List<T> list()
//让我们通过按需加载数据(懒惰)来迭代结果。数据未缓存。必须关闭
public CloseableListIterator<T> listIterator()
//实体按需加载到内存中。首次访问列表中的元素后,将加载并缓存该元素以供将来使用。必须关闭。
public LazyList<T> listLazy()
//实体的“虚拟”列表:对列表元素的任何访问都会导致从数据库加载其数据。必须关闭。
public LazyList<T> listLazyUncached()
- 判断条件
//等于
eq()
//不等于
notEq()
//值等于
like()
//取中间范围
between()
//in命令
in()
//not in 命令
notIn()
//大于
gt()
//小于
lt()
//大于等于
ge()
//小于等于
le()
//为空
isNull()
//不为空
isNotNull()
- 查询所有
// 查询所有
List<Student> list = studentDao.queryBuilder().list();
Log.e(TAG, "onCreate: "+list.size());
- And查询
// And条件查询
List<Student> list = studentDao.queryBuilder()
.where(StudentDao.Properties.MId.eq(2))
.where(StudentDao.Properties.ClassName.eq("一年级"))
.list();
Log.e(TAG, "onCreate: "+list.size());
- Or查询
// Or条件查询
List<Student> list = studentDao.queryBuilder()
.whereOr(StudentDao.Properties.ClassName.eq("一年级"),
StudentDao.Properties.MId.eq(2))
.list();
Log.e(TAG, "onCreate: "+list.size());
- 偏移查询
// 查询10条数据、偏移5条数据
List<Student> list = studentDao.queryBuilder()
.offset(5)
.limit(10)
.list();
Log.e(TAG, "onCreate: "+list.size());
- 模糊查询
// 模糊查询(%级% 代表“级”前面和后面可能存在字符,也可以不存在)
// 一% 表示以一开头的字符串
List<Student> list = studentDao.queryBuilder()
.where(StudentDao.Properties.ClassName.like("%级%"))
.list();
Log.e(TAG, "onCreate: "+list.size());
- 排序
// 返回按照年龄倒序排序
List<Student> list = studentDao.queryBuilder()
.orderDesc(StudentDao.Properties.MAge)
.list();
Log.e(TAG, "onCreate: "+list.size());
- in
// in操作符
List<Student> list = studentDao.queryBuilder()
.where(StudentDao.Properties.MId.in(1,2,3))
.list();
Log.e(TAG, "onCreate: "+list.size());
- sql语句查询
// sql语句查询
List<Student> list = studentDao.queryBuilder()
.where(new WhereCondition.StringCondition(StudentDao.Properties.ClassName.columnName+"=?","一年级"))
.list();
Log.e(TAG, "onCreate: "+list.size());
<a name="67ZbY"></a>
(3)更新
- 常用API
public void update(T entity)
public void updateInTx(T... entities)
public void updateInTx(Iterable<T> entities)
- 更新会导致其他字段置为空
Student student = new Student();
student.setMId("1");
student.setClassName("高一");
studentDao.update(student);
- 查询后再更新,就不会导致其他字段清空
// 先查询后更新
Student load = studentDao.load("0");
load.setHobby("足球");
studentDao.update(load);
(4)保存
- save
public void save(T entity)
public void saveInTx(T... entities)
public void saveInTx(Iterable<T> entities)
- 传入一个实体entity,如果entity的主键在表中已存在,则更新已有数据,反之,则插入新数据
public void save(T entity) {
if (hasKey(entity)) {
update(entity);
} else {
insert(entity);
}
}
<a name="wxGaC"></a>
(5)删除
- 常用方法
//根据实体,删除一条数据(默认根据主键删除数据)
public void delete(T entity)
//删除所有数据
public void deleteAll()
//根据key删除数据
public void deleteByKey(K key)
//删除多条数据
public void deleteInTx(T... entities)
//根据主键数组,删除多条数据
public void deleteByKeyInTx(K... keys)
//根据集合,删除多条数据
public void deleteByKeyInTx(Iterable<K> keys)