贴一下做体温统计app时候用到的几个dao层查询方法,以后可能会需要。
有两张数据表 一张记录体温信息(Tem) 一张记录用户信息(User)
主要就是sqlite的增删改查
1.体温信息相关方法(TemDao)
增加体温信息
public boolean insert(TemBean temBean){//增 SQLiteDatabase db = helper.getReadableDatabase(); Date date0 = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm"); String date = simpleDateFormat.format(date0); ContentValues values = new ContentValues(); values.put("name",temBean.getName()); values.put("condition",temBean.getCondition()); values.put("date",date); values.put("heat",temBean.getHeat()); values.put("place",temBean.getPlace()); values.put("health",temBean.getHealth()); long insert = db.insert("teminfo",null,values); //System.out.println(temBean.getPlace()); return insert>0; }
更改信息
public boolean update(TemBean temBean){//改 SQLiteDatabase db = helper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name",temBean.getName()); values.put("condition",temBean.getCondition()); values.put("date",temBean.getDate()); values.put("heat",temBean.getHeat()); values.put("place",temBean.getPlace()); long update = db.update("teminfo",values,"id=?",new String[]{temBean.getId()+""}); System.out.println(temBean.getId()); return update>0; }
查询信息(全部)
public List<TemBean> queryAll(){//查 List<TemBean> list = new ArrayList<>(); TemBean bean = null; SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("teminfo",null,null,null,null,null,null); if(c != null && c.getCount()>0){ list = new ArrayList<>(); while(c.moveToNext()){ bean = new TemBean(); bean.setId(c.getInt(c.getColumnIndex("id"))); bean.setName(c.getString(c.getColumnIndex("name"))); bean.setCondition(c.getString(c.getColumnIndex("condition"))); bean.setDate(c.getString(c.getColumnIndex("date"))); bean.setHeat(c.getString(c.getColumnIndex("heat"))); bean.setPlace(c.getString(c.getColumnIndex("place"))); list.add(bean); } } return list; }
查询信息(根据'姓名'条件,返回的是list)
public List<TemBean> queryOne(String name){//根据姓名查单独一个人的体温信息 List<TemBean> list = new ArrayList<>(); TemBean bean = null; SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("teminfo",null,"name=?",new String[]{name},null,null,null); if(c != null && c.getCount()>0){ list = new ArrayList<>(); while(c.moveToNext()){ bean = new TemBean(); bean.setId(c.getInt(c.getColumnIndex("id"))); bean.setName(c.getString(c.getColumnIndex("name"))); bean.setCondition(c.getString(c.getColumnIndex("condition"))); bean.setDate(c.getString(c.getColumnIndex("date"))); bean.setHeat(c.getString(c.getColumnIndex("heat"))); bean.setPlace(c.getString(c.getColumnIndex("place"))); bean.setHealth(c.getString(c.getColumnIndex("health"))); list.add(bean); } } return list; }
查询信息(根据'日期'条件模糊查询,返回的是int)
public int normalnum(){//查询体温正常人数,小于37度 SQLiteDatabase db = helper.getReadableDatabase(); Date date0=new Date(); SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy年MM月dd日"); String date = simpleDateFormat.format(date0); int count=0;//统计人数用 Cursor c = db.rawQuery("select heat from teminfo where date like ?",new String[]{"%"+date+"%"}); while(c.moveToNext()){ String heat = c.getString(c.getColumnIndex("heat")); double heat1 = Double.parseDouble(heat); System.out.println(heat1); //int heat1 = Integer.valueOf(heat).intValue(); if(heat1<37.0){ count++; } } return count; }
2.用户信息相关方法(UserDao)
增加一名用户信息
public boolean insert(TemBean temBean){//增 SQLiteDatabase db = helper.getReadableDatabase(); Date date0 = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm"); String date = simpleDateFormat.format(date0); ContentValues values = new ContentValues(); values.put("name",temBean.getName()); values.put("condition",temBean.getCondition()); values.put("date",date); values.put("heat",temBean.getHeat()); values.put("place",temBean.getPlace()); values.put("health",temBean.getHealth()); long insert = db.insert("teminfo",null,values); //System.out.println(temBean.getPlace()); return insert>0; }
查询用户总数(本质为返回数据表的行数)
public int usernum(){//查询用户总数 SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.rawQuery("select count(*) from userinfo",null); c.moveToFirst(); int count = c.getInt(0); c.close(); return count; }
根据'手机号'条件查询(返回String类型的'name')
public String queryNameByPhone(String phone){//按条件查 SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("userinfo",null,"phone=?",new String[]{phone},null,null,null); if(c != null && c.getCount()>0){ while(c.moveToNext()){ String name = c.getString(c.getColumnIndex("name")); return name; } } return "false"; }
根据'手机号'条件查询其所有属性(返回list表)
public List<UserBean> queryAllByPhone(String phone){//查 List<UserBean> list = new ArrayList<>(); UserBean bean = null; SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = db.query("userinfo",null,"phone=?",new String[]{phone},null,null,null); if(c != null && c.getCount()>0){ list = new ArrayList<>(); while(c.moveToNext()){ bean = new UserBean(); bean.setId(c.getInt(c.getColumnIndex("id"))); bean.setUid(c.getString(c.getColumnIndex("uid"))); bean.setName(c.getString(c.getColumnIndex("name"))); bean.setPhone(c.getString(c.getColumnIndex("phone"))); bean.setGrade(c.getString(c.getColumnIndex("grade"))); list.add(bean); } } return list; }
总结:感觉与'查询'有关的方法比较难写,查询的种类比较多,既涉及模糊查询,又需要分别根据不同的条件或者返回值类型,分别写查询方法,比较难搞。之前用MySQL和安卓用的SQLite,增删改查也有点不一样,有点难顶。