Search in sources :

Example 11 with DbException

use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.

the class DbFragment method testDb.

@OnClick(R.id.db_test_btn)
public void testDb(View view) {
    String temp = "";
    Parent parent = new Parent();
    parent.name = "测试" + System.currentTimeMillis();
    parent.setAdmin(true);
    parent.setEmail("wyouflf@gmail.com");
    try {
        //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
        DbUtils db = DbUtils.create(this.getActivity());
        db.configAllowTransaction(true);
        db.configDebug(true);
        Child child = new Child();
        child.name = "child' name";
        //db.saveBindingId(parent);
        //child.parent = new ForeignLazyLoader<Parent>(Child.class, "parentId", parent.getId());
        //child.parent = parent;
        Parent test = db.findFirst(Selector.from(Parent.class).where("id", "in", new int[] { 1, 3, 6 }));
        //Parent test = db.findFirst(Selector.from(Parent.class).where("id", "between", new String[]{"1", "5"}));
        if (test != null) {
            child.parent = test;
            temp += "first parent:" + test + "\n";
            resultText.setText(temp);
        } else {
            child.parent = parent;
        }
        parent.setTime(new Date());
        parent.setDate(new java.sql.Date(new Date().getTime()));
        //保存对象关联数据库生成的id
        db.saveBindingId(child);
        //.where(WhereBuilder.b("name", "=", "child' name")));
        List<Child> children = db.findAll(Selector.from(Child.class));
        temp += "children size:" + children.size() + "\n";
        resultText.setText(temp);
        if (children.size() > 0) {
            temp += "last children:" + children.get(children.size() - 1) + "\n";
            resultText.setText(temp);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.add(Calendar.HOUR, 3);
        List<Parent> list = db.findAll(Selector.from(Parent.class).where("id", "<", 54).and("time", ">", calendar.getTime()).orderBy("id").limit(10));
        temp += "find parent size:" + list.size() + "\n";
        resultText.setText(temp);
        if (list.size() > 0) {
            temp += "last parent:" + list.get(list.size() - 1) + "\n";
            resultText.setText(temp);
        }
        //parent.name = "hahaha123";
        //db.update(parent);
        Parent entity = db.findById(Parent.class, child.parent.getId());
        temp += "find by id:" + entity.toString() + "\n";
        resultText.setText(temp);
        List<DbModel> dbModels = db.findDbModelAll(Selector.from(Parent.class).groupBy("name").select("name", "count(name) as count"));
        temp += "group by result:" + dbModels.get(0).getDataMap() + "\n";
        resultText.setText(temp);
    } catch (DbException e) {
        temp += "error :" + e.getMessage() + "\n";
        resultText.setText(temp);
    }
}
Also used : Parent(com.lidroid.xutils.sample.entities.Parent) Calendar(java.util.Calendar) Date(java.util.Date) DbException(com.lidroid.xutils.exception.DbException) DbUtils(com.lidroid.xutils.DbUtils) DbModel(com.lidroid.xutils.db.table.DbModel) Child(com.lidroid.xutils.sample.entities.Child) OnClick(com.lidroid.xutils.view.annotation.event.OnClick)

Example 12 with DbException

use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.

the class Foreign method setValue2Entity.

@SuppressWarnings("unchecked")
@Override
public void setValue2Entity(Object entity, Cursor cursor, int index) {
    Object fieldValue = foreignColumnConverter.getFieldValue(cursor, index);
    if (fieldValue == null)
        return;
    Object value = null;
    Class<?> columnType = columnField.getType();
    if (columnType.equals(ForeignLazyLoader.class)) {
        value = new ForeignLazyLoader(this, fieldValue);
    } else if (columnType.equals(List.class)) {
        try {
            value = new ForeignLazyLoader(this, fieldValue).getAllFromDb();
        } catch (DbException e) {
            LogUtils.e(e.getMessage(), e);
        }
    } else {
        try {
            value = new ForeignLazyLoader(this, fieldValue).getFirstFromDb();
        } catch (DbException e) {
            LogUtils.e(e.getMessage(), e);
        }
    }
    if (setMethod != null) {
        try {
            setMethod.invoke(entity, value);
        } catch (Throwable e) {
            LogUtils.e(e.getMessage(), e);
        }
    } else {
        try {
            this.columnField.setAccessible(true);
            this.columnField.set(entity, value);
        } catch (Throwable e) {
            LogUtils.e(e.getMessage(), e);
        }
    }
}
Also used : ForeignLazyLoader(com.lidroid.xutils.db.sqlite.ForeignLazyLoader) List(java.util.List) DbException(com.lidroid.xutils.exception.DbException)

Aggregations

DbException (com.lidroid.xutils.exception.DbException)12 Cursor (android.database.Cursor)8 DbModel (com.lidroid.xutils.db.table.DbModel)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Table (com.lidroid.xutils.db.table.Table)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 DbUtils (com.lidroid.xutils.DbUtils)1 FinderLazyLoader (com.lidroid.xutils.db.sqlite.FinderLazyLoader)1 ForeignLazyLoader (com.lidroid.xutils.db.sqlite.ForeignLazyLoader)1 Child (com.lidroid.xutils.sample.entities.Child)1 Parent (com.lidroid.xutils.sample.entities.Parent)1 OnClick (com.lidroid.xutils.view.annotation.event.OnClick)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1