greenDao辅助类

/**

* 数据库操作辅助类

*/

public classDbReUser {

private static finalStringTAG= DbReUser.class.getSimpleName();

private staticDbReUserinstance;

private staticContextappContext;

privateDaoSessionmDaoSession;

privateReuserDaoReuserDao;

privateListlist;

publicDbReUser() {

//TODO Auto-generated constructor stub

}

public staticDbReUsergetInstance(Context context) {

if(instance==null) {

instance=newDbReUser();

if(appContext==null) {

appContext= context.getApplicationContext();

}

instance.mDaoSession= Myapplication.getDaoSession(context);

instance.ReuserDao=instance.mDaoSession.getReuserDao();

}

returninstance;

}

publicReuserloadReuser(longid) {

returnReuserDao.load(id);

}

publicListloadAllReuser() {

//    return ReuserDao.loadAll();

QueryBuilder query =mDaoSession.queryBuilder(Reuser.class);

List all = query.list();

returnall;

}

publicListqueryReuser(String where,String... params) {

returnReuserDao.queryRaw(where,params);

}

public longsaveReuser(Reuser Reuser) {

returnReuserDao.insertOrReplace(Reuser);

}

public voidsaveReuserLists(finalList list) {

if(list ==null|| list.isEmpty()) {

return;

}

ReuserDao.getSession().runInTx(newRunnable() {

@Override

public voidrun() {

for(inti =0;i

Reuser Reuser =list.get(i);

ReuserDao.insertOrReplace(Reuser);

}

}

});

}

public voiddeleteAllReuser() {

ReuserDao.deleteAll();

}

public voiddeleteReuser(longid) {

ReuserDao.deleteByKey(id);

}

public voiddeleteReuser(Reuser Reuser) {

ReuserDao.delete(Reuser);

}

//查询某个表是否包含某个id

public booleanisSaved(intID) {

QueryBuilder qb =ReuserDao.queryBuilder();

qb.where(com.vd.tank.data.dao.ReuserDao.Properties.Id.eq(ID));

qb.buildCount().count();

returnqb.buildCount().count() >0?true:false;

}

public booleanisName(String name) {

QueryBuilder qb =ReuserDao.queryBuilder();

qb.where(com.vd.tank.data.dao.ReuserDao.Properties.Name.eq(name));

qb.buildCount().count();

returnqb.buildCount().count() >0?true:false;

}

//获取整个表的数据集合

publicListgetReuserList() {

returnReuserDao.loadAll();

}

//通过一个字段值查找对应的另一个字段值

// public long getReuserId(String text) {

//    QueryBuilder qb = ReuserDao.queryBuilder();

//    qb.where(Properties.Text.eq(text));

//    if (qb.list().size() > 0) {

//      return qb.list().get(0).getId();

//    } else {

//      return -1;

//    }

// }

//查找所有TEXT属性为text,并且id排序(降序)的list.

// public List getReuserLidtByTextIdAsc(String text) {

//    List list = ReuserDao.queryBuilder().where(Properties.Text.eq(text)).orderDesc(Properties.Id).list();

//    return list;

// }

//获取TEXT字段为text,并且COMMENT字段为comment的数据集合

// public List getSupportReuserList(String text, String comment) {

//    QueryBuilder qb = ReuserDao.queryBuilder();

//    qb.where(qb.and(Properties.Text.eq(text), Properties.Comment.eq(comment)));

//    qb.orderAsc(Properties.Id);

//    return qb.list();

// }

//获取DATE字段为comment,并且AGE字段>=age的数据集合

// public List getSupportReuserList(String comment, int age) {

//    QueryBuilder qb = ReuserDao.queryBuilder();

//    qb.where(Properties.Comment.eq(comment), qb.or(Properties.Age.gt(age), Properties.Age.eq(age)));

//    return qb.list();

// }

//讲Reuser插入表中

public voidaddToReuserTable(String name,String pwd) {

//    ReuserDao.insert(Reuser);

Reuser user =newReuser(null,name,pwd);

ReuserDao.insertOrReplace(user);

//    ReuserDao.insertInTx(Reuser);

}

//delete sql data

public voiddeleteSQLite(Long id) {

ReuserDao.deleteByKey(id);

//delete sql all;

//        userDao.deleteAll();

}

//update sql data

public voidupDateSQLite(longid,String name,String pwd) {

ReuserDao.update(newReuser(id,name,pwd));

}

publicListsearchname(String name) {

list=newArrayList<>();

QueryBuilder qb =ReuserDao.queryBuilder();

list= qb.where(com.vd.tank.data.dao.ReuserDao.Properties.Name.eq(name)).list();

returnlist;

}

publicLongsearchnameid(String name) {

QueryBuilder queryBuilder =ReuserDao.queryBuilder().where(com.vd.tank.data.dao.ReuserDao.Properties.Name.eq(name));

Reuser reuser = queryBuilder.unique();

returnreuser.getId();

}

publicStringsearchpwd(String name) {

QueryBuilder queryBuilder =ReuserDao.queryBuilder().where(com.vd.tank.data.dao.ReuserDao.Properties.Name.eq(name));

Reuser reuser = queryBuilder.unique();

returnreuser.getPassword();

}

public voiddeletename(String name) {

QueryBuilder qb =ReuserDao.queryBuilder();

DeleteQuery db = qb.where(com.vd.tank.data.dao.ReuserDao.Properties.Name.eq(name)).buildDelete();

db.executeDeleteWithoutDetachingEntities();

}

//search sql data

public voidsearchSQLite(longid) {

QueryBuilder queryBuilder =ReuserDao.queryBuilder().where(com.vd.tank.data.dao.ReuserDao.Properties.Id.eq(id));

// .list() Returns a collection of entity classes

//        List user = queryBuilder.list();

//  If you only want results , use .unique() method

//  Reuser reuser = queryBuilder.unique();

//  reuser.getName();

//        new AlertDialog

//                .Builder(this)

//                .setMessage(user != null && user.size() > 0 ? user.get(0).getName() + "--" + user.get(0).getPassword() : "data null")

//                .setPositiveButton("ok", null).create().show();

}

}

你可能感兴趣的:(greenDao辅助类)