(1)插入
- 常用API
//這是最簡單的插入語句,新增一行數(shù)據(jù)哪痰,返回值為行號
public long insert(T entity)
//傳遞一個(gè)數(shù)組,新增多行數(shù)據(jù)
public void insertInTx(T... entities)
//傳遞一個(gè)集合,新增多行數(shù)據(jù)
public void insertInTx(Iterable<T> entities)
//傳遞一個(gè)集合,新增多行數(shù)據(jù)典徊,setPrimaryKey:是否設(shè)置主鍵
public void insertInTx(Iterable<T> entities, boolean setPrimaryKey)
//將給定的實(shí)體插入數(shù)據(jù)庫藏研,若此實(shí)體類存在,則覆蓋
public long insertOrReplace(T entity)
//使用事務(wù)操作嚼黔,將給定的實(shí)體插入數(shù)據(jù)庫,若此實(shí)體類存在惜辑,則覆蓋
public void insertOrReplaceInTx(T... entities)
//使用事務(wù)操作唬涧,將給定的實(shí)體插入數(shù)據(jù)庫,若此實(shí)體類存在盛撑,則覆蓋
public void insertOrReplaceInTx(Iterable<T> entities)
//使用事務(wù)操作碎节,將給定的實(shí)體插入數(shù)據(jù)庫,若此實(shí)體類存在抵卫,則覆蓋狮荔,并設(shè)置是否設(shè)定主鍵
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 );
}
<a name="g8Bsn"></a>
(2)查詢
- Dao查詢常用方法
//根據(jù)主鍵來查詢一條數(shù)據(jù)
public T load(K key)
//根據(jù)行號來查詢一條數(shù)據(jù),行號從1開始
public T loadByRowId(long rowId)
//查詢表中所有的數(shù)據(jù)
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)
//偏移結(jié)果起始位,配合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)
//本地化字符串排序婚瓜,用于加密數(shù)據(jù)庫無效
public QueryBuilder<T> preferLocalizedStringOrder()
//自定義字符串排序,默認(rèn)不區(qū)分大小寫
public QueryBuilder<T> stringOrderCollation(String stringOrderCollation)
- 返回結(jié)果的方法
//值返回一條結(jié)果巡雨,如果返回結(jié)果的數(shù)量>1闰渔,則報(bào)錯(cuò)。如果返回結(jié)果的數(shù)量等于0铐望,那么返回null冈涧。
public T unique()
//值返回一條結(jié)果,如果返回結(jié)果的數(shù)量>1或=0正蛙,則報(bào)錯(cuò)督弓。
public T uniqueOrThrow()
//返回一個(gè)集合
public List<T> list()
//讓我們通過按需加載數(shù)據(jù)(懶惰)來迭代結(jié)果。數(shù)據(jù)未緩存乒验。必須關(guān)閉
public CloseableListIterator<T> listIterator()
//實(shí)體按需加載到內(nèi)存中愚隧。首次訪問列表中的元素后,將加載并緩存該元素以供將來使用锻全。必須關(guān)閉狂塘。
public LazyList<T> listLazy()
//實(shí)體的“虛擬”列表:對列表元素的任何訪問都會導(dǎo)致從數(shù)據(jù)庫加載其數(shù)據(jù)录煤。必須關(guān)閉。
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條數(shù)據(jù)荞胡、偏移5條數(shù)據(jù)
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)
- 更新會導(dǎo)致其他字段置為空
Student student = new Student();
student.setMId("1");
student.setClassName("高一");
studentDao.update(student);
- 查詢后再更新,就不會導(dǎo)致其他字段清空
// 先查詢后更新
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)
- 傳入一個(gè)實(shí)體entity泪漂,如果entity的主鍵在表中已存在廊营,則更新已有數(shù)據(jù),反之萝勤,則插入新數(shù)據(jù)
public void save(T entity) {
if (hasKey(entity)) {
update(entity);
} else {
insert(entity);
}
}
<a name="wxGaC"></a>
(5)刪除
- 常用方法
//根據(jù)實(shí)體露筒,刪除一條數(shù)據(jù)(默認(rèn)根據(jù)主鍵刪除數(shù)據(jù))
public void delete(T entity)
//刪除所有數(shù)據(jù)
public void deleteAll()
//根據(jù)key刪除數(shù)據(jù)
public void deleteByKey(K key)
//刪除多條數(shù)據(jù)
public void deleteInTx(T... entities)
//根據(jù)主鍵數(shù)組,刪除多條數(shù)據(jù)
public void deleteByKeyInTx(K... keys)
//根據(jù)集合敌卓,刪除多條數(shù)據(jù)
public void deleteByKeyInTx(Iterable<K> keys)