Search in sources :

Example 1 with DbException

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

the class DbUtils method dropDb.

public void dropDb() throws DbException {
    Cursor cursor = execQuery("SELECT name FROM sqlite_master WHERE type='table' AND name<>'sqlite_sequence'");
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                try {
                    String tableName = cursor.getString(0);
                    execNonQuery("DROP TABLE " + tableName);
                    Table.remove(this, tableName);
                } catch (Throwable e) {
                    LogUtils.e(e.getMessage(), e);
                }
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtils.closeQuietly(cursor);
        }
    }
}
Also used : Cursor(android.database.Cursor) DbException(com.lidroid.xutils.exception.DbException)

Example 2 with DbException

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

the class DbUtils method getLastAutoIncrementId.

//************************************************ tools ***********************************
private long getLastAutoIncrementId(String tableName) throws DbException {
    long id = -1;
    Cursor cursor = execQuery("SELECT seq FROM sqlite_sequence WHERE name='" + tableName + "'");
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                id = cursor.getLong(0);
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtils.closeQuietly(cursor);
        }
    }
    return id;
}
Also used : Cursor(android.database.Cursor) DbException(com.lidroid.xutils.exception.DbException)

Example 3 with DbException

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

the class DbUtils method findFirst.

@SuppressWarnings("unchecked")
public <T> T findFirst(Selector selector) throws DbException {
    if (!tableIsExist(selector.getEntityType()))
        return null;
    String sql = selector.limit(1).toString();
    long seq = CursorUtils.FindCacheSequence.getSeq();
    findTempCache.setSeq(seq);
    Object obj = findTempCache.get(sql);
    if (obj != null) {
        return (T) obj;
    }
    Cursor cursor = execQuery(sql);
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                T entity = (T) CursorUtils.getEntity(this, cursor, selector.getEntityType(), seq);
                findTempCache.put(sql, entity);
                return entity;
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtils.closeQuietly(cursor);
        }
    }
    return null;
}
Also used : Cursor(android.database.Cursor) DbException(com.lidroid.xutils.exception.DbException)

Example 4 with DbException

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

the class Finder method setValue2Entity.

@Override
public void setValue2Entity(Object entity, Cursor cursor, int index) {
    Object value = null;
    Class<?> columnType = columnField.getType();
    Object finderValue = TableUtils.getColumnOrId(entity.getClass(), this.valueColumnName).getColumnValue(entity);
    if (columnType.equals(FinderLazyLoader.class)) {
        value = new FinderLazyLoader(this, finderValue);
    } else if (columnType.equals(List.class)) {
        try {
            value = new FinderLazyLoader(this, finderValue).getAllFromDb();
        } catch (DbException e) {
            LogUtils.e(e.getMessage(), e);
        }
    } else {
        try {
            value = new FinderLazyLoader(this, finderValue).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 : List(java.util.List) FinderLazyLoader(com.lidroid.xutils.db.sqlite.FinderLazyLoader) DbException(com.lidroid.xutils.exception.DbException)

Example 5 with DbException

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

the class DbUtils method findDbModelAll.

public List<DbModel> findDbModelAll(DbModelSelector selector) throws DbException {
    if (!tableIsExist(selector.getEntityType()))
        return null;
    List<DbModel> dbModelList = new ArrayList<DbModel>();
    Cursor cursor = execQuery(selector.toString());
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                dbModelList.add(CursorUtils.getDbModel(cursor));
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtils.closeQuietly(cursor);
        }
    }
    return dbModelList;
}
Also used : ArrayList(java.util.ArrayList) DbModel(com.lidroid.xutils.db.table.DbModel) Cursor(android.database.Cursor) 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