Search in sources :

Example 6 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class DbManagerImpl method execNonQuery.

@Override
public void execNonQuery(SqlInfo sqlInfo) throws DbException {
    SQLiteStatement statement = null;
    try {
        statement = sqlInfo.buildStatement(database);
        statement.execute();
    } catch (Throwable e) {
        throw new DbException(e);
    } finally {
        if (statement != null) {
            try {
                statement.releaseReference();
            } catch (Throwable ex) {
                LogUtil.e(ex.getMessage(), ex);
            }
        }
    }
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) DbException(org.xutils.ex.DbException)

Example 7 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class DbFragment method onTestDb2Click.

@Event(R.id.btn_test_db2)
private void onTestDb2Click(View view) {
    tv_db_result.setText("wait...");
    x.task().run(new // 异步执行
    Runnable() {

        @Override
        public void run() {
            DbManager db = x.getDb(daoConfig);
            String result = "";
            List<Parent> parentList = new ArrayList<Parent>();
            for (int i = 0; i < 1000; i++) {
                Parent parent = new Parent();
                parent.setAdmin(true);
                parent.setDate(new java.sql.Date(1234));
                parent.setTime(new Date());
                parent.setEmail(i + "_@qq.com");
                parentList.add(parent);
            }
            long start = System.currentTimeMillis();
            for (Parent parent : parentList) {
                try {
                    db.save(parent);
                } catch (DbException ex) {
                    ex.printStackTrace();
                }
            }
            result += "插入1000条数据:" + (System.currentTimeMillis() - start) + "ms\n";
            start = System.currentTimeMillis();
            try {
                parentList = db.selector(Parent.class).orderBy("id", true).limit(1000).findAll();
            } catch (DbException ex) {
                ex.printStackTrace();
            }
            result += "查找1000条数据:" + (System.currentTimeMillis() - start) + "ms\n";
            start = System.currentTimeMillis();
            try {
                db.delete(parentList);
            } catch (DbException ex) {
                ex.printStackTrace();
            }
            result += "删除1000条数据:" + (System.currentTimeMillis() - start) + "ms\n";
            // 批量插入
            parentList = new ArrayList<Parent>();
            for (int i = 0; i < 1000; i++) {
                Parent parent = new Parent();
                parent.setAdmin(true);
                parent.setDate(new java.sql.Date(1234));
                parent.setTime(new Date());
                parent.setEmail(i + "_@qq.com");
                parentList.add(parent);
            }
            start = System.currentTimeMillis();
            try {
                db.save(parentList);
            } catch (DbException ex) {
                ex.printStackTrace();
            }
            result += "批量插入1000条数据:" + (System.currentTimeMillis() - start) + "ms\n";
            try {
                parentList = db.selector(Parent.class).orderBy("id", true).limit(1000).findAll();
                db.delete(parentList);
            } catch (DbException ex) {
                ex.printStackTrace();
            }
            final String finalResult = result;
            x.task().post(new // UI同步执行
            Runnable() {

                @Override
                public void run() {
                    tv_db_result.setText(finalResult);
                }
            });
        }
    });
}
Also used : Parent(org.xutils.sample.db.Parent) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DbManager(org.xutils.DbManager) Date(java.util.Date) DbException(org.xutils.ex.DbException) Event(org.xutils.view.annotation.Event)

Example 8 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class DbManagerImpl method getInstance.

public static synchronized DbManager getInstance(DaoConfig daoConfig) {
    if (daoConfig == null) {
        //使用默认配置
        daoConfig = new DaoConfig();
    }
    DbManagerImpl dao = DAO_MAP.get(daoConfig);
    if (dao == null) {
        dao = new DbManagerImpl(daoConfig);
        DAO_MAP.put(daoConfig, dao);
    } else {
        dao.daoConfig = daoConfig;
    }
    // update the database if needed
    SQLiteDatabase database = dao.database;
    int oldVersion = database.getVersion();
    int newVersion = daoConfig.getDbVersion();
    if (oldVersion != newVersion) {
        if (oldVersion != 0) {
            DbUpgradeListener upgradeListener = daoConfig.getDbUpgradeListener();
            if (upgradeListener != null) {
                upgradeListener.onUpgrade(dao, oldVersion, newVersion);
            } else {
                try {
                    dao.dropDb();
                } catch (DbException e) {
                    LogUtil.e(e.getMessage(), e);
                }
            }
        }
        database.setVersion(newVersion);
    }
    return dao;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DbException(org.xutils.ex.DbException)

Example 9 with DbException

use of org.xutils.ex.DbException 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 10 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class DbManagerImpl method findById.

@Override
@SuppressWarnings("unchecked")
public <T> T findById(Class<T> entityType, Object idValue) throws DbException {
    TableEntity<T> table = this.getTable(entityType);
    if (!table.tableIsExist())
        return null;
    Selector selector = Selector.from(table).where(table.getId().getName(), "=", idValue);
    String sql = selector.limit(1).toString();
    Cursor cursor = execQuery(sql);
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                return CursorUtils.getEntity(table, cursor);
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtil.closeQuietly(cursor);
        }
    }
    return null;
}
Also used : Cursor(android.database.Cursor) DbException(org.xutils.ex.DbException)

Aggregations

DbException (org.xutils.ex.DbException)16 Cursor (android.database.Cursor)8 ColumnEntity (org.xutils.db.table.ColumnEntity)3 ArrayList (java.util.ArrayList)2 ProcessLock (org.xutils.common.util.ProcessLock)2 DbModel (org.xutils.db.table.DbModel)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 List (java.util.List)1 DbManager (org.xutils.DbManager)1 KeyValue (org.xutils.common.util.KeyValue)1 FileLockedException (org.xutils.ex.FileLockedException)1 Parent (org.xutils.sample.db.Parent)1 Event (org.xutils.view.annotation.Event)1