| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package com.xzjmyk.pm.basepedo.utils;
- import android.content.Context;
- import com.litesuits.orm.LiteOrm;
- import com.litesuits.orm.db.assit.QueryBuilder;
- import com.litesuits.orm.db.model.ConflictAlgorithm;
- import com.xzjmyk.pm.activity.ui.erp.util.ListUtils;
- import java.util.List;
- /**
- * Created by xf on 2016/1/31.
- */
- public class DbUtils {
- public static String DB_NAME;
- public static LiteOrm liteOrm;
- public static void createDb(Context _activity, String DB_NAME) {
- DB_NAME = DB_NAME + ".db";
- if (liteOrm == null) {
- liteOrm = LiteOrm.newCascadeInstance(_activity, DB_NAME);
- liteOrm.setDebugged(true);
- }
- }
- public static LiteOrm getLiteOrm() {
- return liteOrm;
- }
- /**
- * 插入一条记录
- *
- * @param t
- */
- public static <T> void insert(T t) {
- liteOrm.save(t);
- }
- /**
- * 插入所有记录
- *
- * @param list
- */
- public static <T> void insertAll(List<T> list) {
- liteOrm.save(list);
- }
- /**
- * 查询所有
- *
- * @param cla
- * @return
- */
- public static <T> List<T> getQueryAll(Class<T> cla) {
- return liteOrm.query(cla);
- }
- /**
- * 查询 某字段 等于 Value的值
- *
- * @param cla
- * @param field
- * @param value
- * @return
- */
- public static <T> List<T> getQueryByWhere(Class<T> cla, String field, String[] value) {
- if (!ListUtils.isEmpty(liteOrm.<T>query(new QueryBuilder(cla).where(field + "=?", value)))){
- return liteOrm.<T>query(new QueryBuilder(cla).where(field + "=?", value));
- }else {
- return null;
- }
- }
- /**
- * 查询 某字段 等于 Value的值 可以指定从1-20,就是分页
- *
- * @param cla
- * @param field
- * @param value
- * @param start
- * @param length
- * @return
- */
- public static <T> List<T> getQueryByWhereLength(Class<T> cla, String field, String[] value, int start, int length) {
- return liteOrm.<T>query(new QueryBuilder(cla).where(field + "=?", value).limit(start, length));
- }
- /**
- * 删除所有 某字段等于 Vlaue的值
- * @param cla
- * @param field
- * @param value
- */
- // public static <T> void deleteWhere(Class<T> cla,String field,String [] value){
- // liteOrm.delete(cla, WhereBuilder.create().where(field + "=?", value));
- // }
- /**
- * 删除所有
- *
- * @param cla
- */
- public static <T> void deleteAll(Class<T> cla) {
- liteOrm.deleteAll(cla);
- }
- /**
- * 仅在以存在时更新
- *
- * @param t
- */
- public static <T> void update(T t) {
- liteOrm.update(t, ConflictAlgorithm.Replace);
- }
- public static <T> void updateALL(List<T> list) {
- liteOrm.update(list);
- }
- public static void closeDb(){
- liteOrm.close();
- }
- }
|