Android GreenDao 使用教程
上一篇 总结了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(List<Note> notes){
daoSession.getNoteDao().insertInTx(notes);
}
GreenDao的更新操作
1:单条更新(唯一性条件是主键相同)
public void update(Note note) throws Exception {
daoSession.update(note);
// daoSession.getNoteDao().update(note);
}
2:批量更新
//批量更新
public void updateBatch(List<Note> notes) throws Exception {
daoSession.getNoteDao().updateInTx(notes);
}
3:原生sql:
public void updateSql(String sql){
// daoSession.getDatabase().execSQL("update .....");
daoSession.getDatabase().execSQL(sql);
}
GreenDao 的删除操作
1:单条删除(唯一性是主见相同)
public void delete(Note note) throws Exception {
daoSession.delete(note);
// daoSession.getNoteDao().delete(note);
}
2: 单条删除 指定主键删除
public void delete(long id) {
daoSession.getNoteDao().deleteByKey(id);
}
3:批量删除
public void deleteBatch(List<Note> notes) {
daoSession.getNoteDao().deleteInTx(notes);
}
4:批量按主键删除
// 批量按主键删除
public void deleteBatchByKey(List<Long> pkIds) {
daoSession.getNoteDao().deleteByKeyInTx(pkIds);
}
5:删除所有
public void deleteAll() throws Exception {
daoSession.deleteAll(Note.class);
// daoSession.getNoteDao().deleteAll();
}
6:原始sqlite语句
daoSession.getDatabase().execSQL(deletesql);
GreenDao 查询操作 也是最复杂的
1:单条查询
//按主键查询
public Note query(long pk) throws Exception {
// daoSession.getNoteDao().load(pk);//按主见查询
//daoSession.getNoteDao().load(rowId);//按行号查询
return daoSession.load(Note.class, pk);
}
2: QueryBuilder 复合查询
public List<Note> queryBuider() 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(long pk) throws Exception {
//select count(*) from tb_note where Content like 'greendao'
return daoSession.getNoteDao().queryBuilder().where(NoteDao.Properties.Content.like("greendao")).buildCount().count();
}
public void queryBuider3() 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();
String title = cursor.getString();
String content = cursor.getString();
String time = cursor.getString(cursor.getColumnIndex(NoteDao.Properties.CreateTime.columnName));
}
} finally {
if (cursor != null)
cursor.close();
}
}
3:查询所有:
public void queryAll()
{
daoSession.loadAll(Note.class);
daoSession.queryBuilder(Note.class).build().list();
daoSession.getNoteDao().queryBuilder().build().list();
daoSession.getNoteDao().loadAll();
}
Android GreenDao 使用教程的更多相关文章
- Android GreenDao使用教程
1.在build.gradle里添加相关依赖 apply plugin: 'org.greenrobot.greendao' buildscript { repositories { mavenCen ...
- Android Studio2.0 教程MAC版 -快捷键篇
本文转至 Android Studio2.0 教程从入门到精通MAC版 - 提高篇 ( OPEN 开发经验库) 第二篇我们开发了一个Hello World应用,并介绍Android Sutdio的界面 ...
- Android图像处理实例教程
Android图像处理实例教程 原始出处 http://vaero.blog.51cto.com/4350852/856750
- android用户界面详尽教程实例
android用户界面详尽教程实例 1.android用户界面之AlarmManager教程实例汇总http://www.apkbus.com/android-48405-1-1.html2.andr ...
- [转]Android Studio系列教程六--Gradle多渠道打包
转自:http://www.stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/ Android Studio系列教程六--Grad ...
- 【Android进阶系列教程】前言
起因 因为初学Android的时候还没有写博客的意识,现在Android的门是入了,正在进阶的道路上行走,但是就这一路也走了不少的弯路.我想,总得来说Android入门还是比较容易的,网络资源比较丰富 ...
- Android Studio使用教程(二)
以下是本次Google I/O大会发布的IDE Android Studio使用教程第二篇: 在Android Studio使用教程(一)中简要介绍了Android Studio的基本使用,包括安装. ...
- Android Studio系列教程六--Gradle多渠道打包
Android Studio系列教程六--Gradle多渠道打包 2015 年 01 月 15 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://stormzh ...
- Android Studio系列教程五--Gradle命令详解与导入第三方包
Android Studio系列教程五--Gradle命令详解与导入第三方包 2015 年 01 月 05 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://s ...
随机推荐
- JDK1.7源码阅读tools包之------ArrayList,LinkedList,HashMap,TreeMap
1.HashMap 特点:基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Has ...
- (转) RabbitMQ学习之发布/订阅(java)
http://blog.csdn.net/zhu_tianwei/article/details/40887733 参考:http://blog.csdn.NET/lmj623565791/artic ...
- vc++元文件的保存,保存图形,重绘图形
1, CMateFileDC 可以用来多次打开自己的画布,这个元文件包含许多接口的命令 当绘制好之后可以用来播放元文件 首先,创建一个CMateFileDC的元文件对象 然后调用Create原函数,创 ...
- centos7的编译安装php5.3 (针对老系统必须安装php5.3才能运行)
大家都知道,centos6以上yum都自带5.4以上的php版本,可是一些老系统必须安装 php-5.3该怎么办呢.下面我来教大家一步步编译安装.看看我踩过的坑. 第一步: 网上下载php5.3的源码 ...
- 实验二:编写输出"Hello World!"
1.首先打开eclipse这个软件,新建Java项目,执行“文件→ 新建→Java项目 ”菜单命令,打开新建Java对话框,在项目名的编辑框中输入项目名编写输出"Hello World!”, ...
- [长期更新]模板&算法学习情况
这里仅作为自我检查用,模板代码请移步其他博文 标+的表示已学完,标?的表示需要进一步学习,标-的表示有计划但未开始学习,标*的表示暂时没有计划学习 数学 ?BSGS +FFT&NTT ?Luc ...
- Javascript解析URL
举个栗子,一个网页的URL为https://i.cnblogs.com/EditPosts.aspx?opt=1,要分离出通信协议.host.port.path.query.hash等值.这时候我们应 ...
- win主机ping不通linux的IP
1.虚拟机的中的linux系统设置成桥接模式 2.点击虚拟机的编辑选择虚拟网络编辑器 3.点击更改设置 4点击还原默认设置即可
- vim+astyle安装使用
astyle下载安装 wget https://sourceforge.net/projects/astyle/files/astyle/astyle%203.1/astyle_3.1_linux.t ...
- Django入门--自定义过滤器与标签
---恢复内容开始--- 为了让Django找到自定义的模板过滤器和模板标签,需要进行文件路径配置,配置方式分为APP目录下配置和项目路径下配置两种方式: 1.在APP目录下配置:针对某个应用特定的自 ...