publicclassLiteOrmDBUtil{publicstaticString DB_NAME;publicstaticLiteOrm liteOrm;publicstaticvoidcreateDb(Context _activity,Object userId){DB_NAME ="pinme_"+ userId.toString() +".db"; liteOrm = LiteOrm.newCascadeInstance(_activity, DB_NAME); liteOrm.setDebugged(true);}publicstaticLiteOrmgetLiteOrm(){returnliteOrm;}/**
* 插入一条记录
* @param t
*/publicstaticvoidinsert(T t){liteOrm.save(t);}/**
* 插入所有记录
* @param list
*/publicstaticvoidinsertAll(Listlist){liteOrm.save(list);}/**
* 查询所有
* @param cla
* @return
*/publicstatic List getQueryAll(Class cla){returnliteOrm.query(cla);}/**
* 查询 某字段 等于 Value的值
* @param cla
* @param field
* @param value
* @return
*/publicstatic List getQueryByWhere(Class cla,String field,String [] value){returnliteOrm.query(newQueryBuilder(cla).where(field +"=?", value));}/**
* 查询 某字段 等于 Value的值 可以指定从1-20,就是分页
* @param cla
* @param field
* @param value
* @param start
* @param length
* @return
*/publicstatic List getQueryByWhereLength(Class cla,String field,String [] value,intstart,intlength){returnliteOrm.query(newQueryBuilder(cla).where(field +"=?", value).limit(start, length));}/**
* 删除所有 某字段等于 Vlaue的值
* @param cla
* @param field
* @param value
*/publicstaticvoiddeleteWhere(Class<T> cla,String field,String [] value){liteOrm.delete(cla, WhereBuilder.create().where(field +"=?", value));}/**
* 删除所有
* @param cla
*/publicstaticvoiddeleteAll(Class<T> cla){liteOrm.deleteAll(cla);}/**
* 仅在以存在时更新
* @param t
*/publicstaticvoidupdate(T t){liteOrm.update(t,ConflictAlgorithm.Replace);}publicstaticvoidupdateALL(Listlist){liteOrm.update(list);}publicstaticvoidText(){//我们把这个对象当做以填充数据的后的对象Conversation mConversation =newConversation();Listlist=newArrayList();for(inti =0; i <10; i++) {list.add(mConversation);}//1、插入单条数据LiteOrmDBUtil.insert(mConversation);//2、插入多条数据LiteOrmDBUtil.insertAll(list);//3、查询Conversation表中所有记录Listlist= LiteOrmDBUtil.getQueryAll(Conversation.class);//4、查询Conversation表中 isVisibility 字段 等于 true 的记录Listlist= LiteOrmDBUtil.getQueryByWhere(Conversation.class, Conversation.ISVISIBILITY,newString[]{"true"});//5、查询Conversation表中 isVisibility 字段 等于 true 的记录,并且只取20条 Listlist= LiteOrmDBUtil.getQueryByWhereLength(Conversation.class, Conversation.ISVISIBILITY,newString[]{"true"},0,20);//6、删除Conversation表中 isVisibility 字段 等于 true 的记录LiteOrmDBUtil.deleteWhere(Conversation.class,Conversation.ISVISIBILITY ,newString[]{"true"});//7、删除所有LiteOrmDBUtil.deleteAll(Conversation.class);}}
网友评论