Search in sources :

Example 1 with DbModel

use of org.xutils.db.table.DbModel in project xUtils3 by wyouflf.

the class Selector method count.

public long count() throws DbException {
    if (!table.tableIsExist())
        return 0;
    DbModelSelector dmSelector = this.select("count(\"" + table.getId().getName() + "\") as count");
    DbModel firstModel = dmSelector.findFirst();
    if (firstModel != null) {
        return firstModel.getLong("count");
    }
    return 0;
}
Also used : DbModel(org.xutils.db.table.DbModel)

Example 2 with DbModel

use of org.xutils.db.table.DbModel in project xUtils3 by wyouflf.

the class CursorUtils method getDbModel.

public static DbModel getDbModel(final Cursor cursor) {
    DbModel result = new DbModel();
    int columnCount = cursor.getColumnCount();
    for (int i = 0; i < columnCount; i++) {
        result.add(cursor.getColumnName(i), cursor.getString(i));
    }
    return result;
}
Also used : DbModel(org.xutils.db.table.DbModel)

Example 3 with DbModel

use of org.xutils.db.table.DbModel in project xUtils3 by wyouflf.

the class DbFragment method onTestDbClick.

@Event(R.id.btn_test_db)
private void onTestDbClick(View view) {
    // 一对多: (本示例的代码)
    // 自己在多的一方(child)保存另一方的(parentId), 查找的时候用parentId查parent或child.
    // 一对一:
    // 在任何一边保存另一边的Id并加上唯一属性: @Column(name = "parentId", property = "UNIQUE")
    // 多对多:
    // 再建一个关联表, 保存两边的id. 查询分两步: 先查关联表得到id, 再查对应表的属性.
    String temp = "";
    try {
        DbManager db = x.getDb(daoConfig);
        Child child = new Child();
        child.setName("child's name");
        Parent test = db.selector(Parent.class).where("id", "in", new int[] { 1, 3, 6 }).findFirst();
        // Parent test = db.selector(Parent.class).where("id", "between", new String[]{"1", "5"}).findFirst();
        if (test != null) {
            child.setParentId(test.getId());
            temp += "first parent:" + test + "\n";
            tv_db_result.setText(temp);
        }
        Parent parent = new Parent();
        parent.name = "测试" + System.currentTimeMillis();
        parent.setAdmin(true);
        parent.setEmail("wyouflf@qq.com");
        parent.setTime(new Date());
        parent.setDate(new java.sql.Date(new Date().getTime()));
        //db.save(parent);
        db.saveBindingId(parent);
        //保存对象关联数据库生成的id
        db.saveBindingId(child);
        List<Child> children = db.selector(Child.class).findAll();
        temp += "children size:" + children.size() + "\n";
        tv_db_result.setText(temp);
        if (children.size() > 0) {
            temp += "last children:" + children.get(children.size() - 1) + "\n";
            tv_db_result.setText(temp);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.add(Calendar.HOUR, 3);
        List<Parent> list = db.selector(Parent.class).where("id", "<", 54).and("time", ">", calendar.getTime()).orderBy("id").limit(10).findAll();
        temp += "find parent size:" + list.size() + "\n";
        tv_db_result.setText(temp);
        if (list.size() > 0) {
            temp += "last parent:" + list.get(list.size() - 1) + "\n";
            tv_db_result.setText(temp);
        }
        // test update
        parent.name = "hahaha123";
        parent.setEmail("wyouflf@gmail.com");
        db.update(parent);
        db.update(parent, "name", "email");
        db.update(Parent.class, WhereBuilder.b("id", "=", 1).and("isAdmin", "=", true), new KeyValue("name", "test_name"), new KeyValue("isAdmin", false));
        Parent entity = child.getParent(db);
        temp += "find by id:" + entity.toString() + "\n";
        tv_db_result.setText(temp);
        List<DbModel> dbModels = db.selector(Parent.class).groupBy("name").select("name", "count(name) as count").findAll();
        temp += "group by result:" + dbModels.get(0).getDataMap() + "\n";
        tv_db_result.setText(temp);
    } catch (Throwable e) {
        temp += "error :" + e.getMessage() + "\n";
        tv_db_result.setText(temp);
    }
}
Also used : KeyValue(org.xutils.common.util.KeyValue) Parent(org.xutils.sample.db.Parent) Calendar(java.util.Calendar) DbManager(org.xutils.DbManager) Date(java.util.Date) DbModel(org.xutils.db.table.DbModel) Child(org.xutils.sample.db.Child) Event(org.xutils.view.annotation.Event)

Example 4 with DbModel

use of org.xutils.db.table.DbModel in project xUtils3 by wyouflf.

the class DbManagerImpl method findDbModelAll.

@Override
public List<DbModel> findDbModelAll(SqlInfo sqlInfo) throws DbException {
    List<DbModel> dbModelList = new ArrayList<DbModel>();
    Cursor cursor = execQuery(sqlInfo);
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                dbModelList.add(CursorUtils.getDbModel(cursor));
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtil.closeQuietly(cursor);
        }
    }
    return dbModelList;
}
Also used : ArrayList(java.util.ArrayList) DbModel(org.xutils.db.table.DbModel) Cursor(android.database.Cursor) DbException(org.xutils.ex.DbException)

Example 5 with DbModel

use of org.xutils.db.table.DbModel in project xUtils3 by wyouflf.

the class DbModelSelector method findAll.

public List<DbModel> findAll() throws DbException {
    TableEntity<?> table = selector.getTable();
    if (!table.tableIsExist())
        return null;
    List<DbModel> result = null;
    Cursor cursor = table.getDb().execQuery(this.toString());
    if (cursor != null) {
        try {
            result = new ArrayList<DbModel>();
            while (cursor.moveToNext()) {
                DbModel entity = CursorUtils.getDbModel(cursor);
                result.add(entity);
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtil.closeQuietly(cursor);
        }
    }
    return result;
}
Also used : DbModel(org.xutils.db.table.DbModel) Cursor(android.database.Cursor) DbException(org.xutils.ex.DbException)

Aggregations

DbModel (org.xutils.db.table.DbModel)5 Cursor (android.database.Cursor)2 DbException (org.xutils.ex.DbException)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 DbManager (org.xutils.DbManager)1 KeyValue (org.xutils.common.util.KeyValue)1 Child (org.xutils.sample.db.Child)1 Parent (org.xutils.sample.db.Parent)1 Event (org.xutils.view.annotation.Event)1