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);
}
}
}
}
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);
}
});
}
});
}
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;
}
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;
}
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;
}
Aggregations