GreenDao封装使用
case R.id.add: //添加数据Person item=new Person(null,"wang","18");PersonHelper.getInstance(this).addInfo(item);break;case R.id.delete://删除id是1的数据PersonHelper.getInstance(this).deleteInfo(1);break;case R.id.delete_table:// 删除这个表PersonHelper.getInstance(this).clearInfo();break;case R.id.update: //修改 id为1的数据//id需要强转为longPerson item2=new Person((long)1,"up","10");PersonHelper.getInstance(this).updateInfo(item2);break;case R.id.query: //查询id为1的name字段的值String name=PersonHelper.getInstance(this).getTypeValue(1);text.setText(name);break;case R.id.query_list: //查询所有数据List<Person> list = PersonHelper.getInstance(this).getInfoList2();Person person = new Person();StringBuilder str = new StringBuilder();//和String 它这个必须new对象。不然就是空指针。for(int i=0;i<list.size();i++){person=list.get(i);String names=person.getName();String ages=person.getAge();Long id=person.getId();str.append(names+id+""); System.out.println("---查询---->"+names + id+"");}if(TextUtils.isEmpty(str)){text.setText("没有数据");}else{text.setText(str);}break;case R.id.query_have: //查询有没有id为1的数据boolean flag=PersonHelper.getInstance(this).isHave(1);if(flag==true){text.setText("true");}else{text.setText("false");}break;}
import android.content.Context;import com.example.greendaos_dao.DaoMaster;
import com.example.greendaos_dao.DaoMaster.OpenHelper;
import com.example.greendaos_dao.DaoSession;public class DbHelper {private static DaoMaster daoMaster;private static DaoSession daoSession;public DbHelper() {}/** * 取得DaoMaster ** @param context * @return*/public static DaoMaster getDaoMaster(Context context) { if (daoMaster == null) {OpenHelper helper = new DaoMaster.DevOpenHelper(context, "my_db", null);daoMaster = new DaoMaster(helper.getWritableDatabase()); } return daoMaster; }/** * 取得DaoSession* * @param context * @return*/public static DaoSession getDaoSession(Context context) {if (daoSession == null) { if (daoMaster == null) { daoMaster = getDaoMaster(context); } daoSession = daoMaster.newSession(); }return daoSession;}
}
import java.util.List;
import java.util.Map;import com.example.greendaos_dao.DaoSession;
import com.example.greendaos_dao.Person;
import com.example.greendaos_dao.PersonDao;
import com.example.greendaos_dao.PersonDao.Properties;import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;
import android.content.Context;
//10个方法
public class PersonHelper {private static Context mContext;private static PersonHelper instance;private PersonDao personDao; //先去掉import包 然后改 PersonDao personDao Person; ctrl+f全修改public PersonHelper() {// TODO Auto-generated constructor stub}public static PersonHelper getInstance(Context context) { if (instance == null){ instance = new PersonHelper();if (mContext == null) { mContext = context; }// 数据库对象 DaoSession daoSession = DbHelper.getDaoSession(mContext);instance.personDao = daoSession.getPersonDao();} return instance; }/** 添加数据 */public void addInfo(Person item){personDao.insert(item);}/** 删除 一条数据,根据id删除 */public void deleteInfo(int Id){personDao.deleteByKey((long) Id);//删除一条note:非常简单明,在onListItemClick方法中可以看到 }/** 删除 可修改变成删除一组数据 */public void deleteInfoList(int id){QueryBuilder<Person> qb = personDao.queryBuilder();//删除的条件DeleteQuery<Person> bd = qb.where(Properties.Id.eq(id)).buildDelete();bd.executeDeleteWithoutDetachingEntities();}/** 删除表内所有数据*/public void clearInfo() {personDao.deleteAll(); }/** 修改*///item中有id,根据id进行修改。下面两中方法都可以用。public void updateInfo(Person item){personDao.insertOrReplace(item);//personDao.update(note2);}/** 多条数据查询 */public List<Person> getInfoList1(){QueryBuilder<Person> qb = personDao.queryBuilder();return qb.list();}/** 查询 获取整个表的数据集合,一句代码就搞定!*/public List<Person> getInfoList2(){return personDao.loadAll();}/** 查询某个Id是否存在 */public boolean isHave(int Id){QueryBuilder<Person> qb = personDao.queryBuilder();qb.where(Properties.Id.eq(Id));qb.buildCount().count();return qb.buildCount().count() > 0 ? true : false;// 查找收藏表}/**单条查询 通过id查找其类型字段值 */public String getTypeValue(int id) { QueryBuilder<Person> qb = personDao.queryBuilder(); qb.where(Properties.Id.eq(id)); //通过id字段查询,具体使用可修改别的字段if (qb.list().size() > 0) { return qb.list().get(0).getName(); //根据id获取它的name字段的值。具体用,具体修改} else { return "0"; } }/** 多条件重查询 */public List<Person> getTJList(int id){QueryBuilder<Person> qb = personDao.queryBuilder();//这是查询条件qb.where(qb.and(Properties.Id.eq(id), Properties.Age.eq(20))); //eq等于。gt大于。ge大于等于qb.orderAsc(Properties.Id);// 排序依据 ASC表示升序排序,DESC表示降序排序return qb.list();}//名字叫“乔”和出生年份(大于1970或(出生年份是1970年,出生月等于或大于10(10月)。
// QueryBuilder qb = userDao.queryBuilder();
// qb.where(Properties.FirstName.eq("Joe"),
// qb.or(Properties.YearOfBirth.gt(1970),
// qb.and(Properties.YearOfBirth.eq(1970), Properties.MonthOfBirth.ge(10))));
// List youngJoes = qb.list();// 比如:查找所有以“Joe”为first name 的用户,并以last name排序:// List joes = userDao.queryBuilder()
// .where(Properties.FirstName.eq("Joe"))
// .orderAsc(Properties.LastName)
// .list();}
GreenDao封装使用相关推荐
- 使用反射+注解封装一个基于Sqlite极简的android数据库框架
数据库 背景 GreenDao 封装 创建数据库 对象映射表 数据库操作 扩展 总结 背景 目前市面上已经有比较好用的数据库框架,比如GreenDao和OrmLite,而且功能也很齐全,那为什么还要多 ...
- greenDao的使用与封装
1. greenDao的使用步骤: 第一步: 在项目的.gradle文件里面添加 buildscript {repositories {mavenCentral()jcenter()}depende ...
- java basedao 封装,GreenDao的实用封装
前面简单介绍了下GreenDao的使用,从前面的介绍看来是不是觉得有点 so easy.对就是这么简单.曾经有位大神说作为一位合格的程序员就要在学习别人的东西时,有点自己的想法.为了表示我是位比较合格 ...
- GreenDao自带异步操作类简析
AsyncSession: GreenDao提供一个异步操作的统一接口类AsyncSession,它提供了你所需要的所有异步操作方法. 你可以通过调用DaoSession#startAsyncSess ...
- Android ORM 框架之 greenDAO 使用心得
前言 我相信,在平时的开发过程中,大家一定会或多或少地接触到 SQLite.然而在使用它时,我们往往需要做许多额外的工作,像编写 SQL 语句与解析查询结果等.所以,适用于 Android 的ORM ...
- Android持久化存储(4)greenDAO的使用
1.背景 在上篇博客介绍SQLite的使用我们能感觉到,虽然Android已经简化了一些SQLite的操作,提供了较为方便的API,但使用中还是需要开发者写一些SQL语言,简单而不简约,有时候只想存储 ...
- GreenDao 工具类 --- 使用 Json 快速生成 Bean、表及其结构,炒鸡快!
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- GreenDao 配置和使用
作为github上star超8k fork超2k的强大框架,很有必要来用一用嘛~~~~~~ 首先奉上GreenDao的github地址https://github.com/greenrobot/gre ...
- Android GreenDao
离线开发 开发工具: androidStudio ,SQLite3 使用开源库:GreenDao 3.2.2 离线已实现: 1. 增.删.改.查 2. 新增表,字段,更改字段,升级数据迁移 3. 从 ...
最新文章
- 001_Spring Data JPA
- 抢跑直播电商双11,快手电商帝国雏形初显
- Wasserstein GAN最新进展:从weight clipping到gradient penalty,更加先进的Lipschitz限制手法
- ubuntu的mysql教程 pdf_Ubuntu上的MySQL字符集设置技巧
- 【今日CS 视觉论文速览】 9 Jan 2019
- 用HashMap去重
- DisplayLink 安装错误
- python求两组同学的平均成绩_Python学习心得2:求平均值
- mysql 重建注册_mysql 重建帐号
- Ansible详解(六)——Ansible palybook基础
- 图片降噪DeNoise AI
- java aspect demo_Spring AOP + Aspect 实现切面编程
- 安全红蓝对抗反制(反捕、画像)
- Maven详细入门及教学视频推荐
- 2022吴恩达机器学习课程——第一课
- [项目管理]关于风险管理的问题和可能过早的幼儿教育
- word批注怎么删除计算机名字,Word批注怎样删掉用户名
- 2020 网络安全重保日记
- 按下鼠标中键跳转新的页面
- 假装自己是“黑客”二