Lite sqlite Database framework on Android which can handle table hierarchies elegantly and efficiently. It support upgrade database seamlessly and subscribe events
of table change too.
1.Handle table hierarchies elegantly.
2.Support upgrade database seamlessly.
3.Support subscribe events of table change.
4.Structured, Efficient and Easy to use, save you out of a lot of redundant code.
1.Download or clone project and import sub module called database into your project. Then implement your daos which should extends from BaseDao:
public class UserDao extends BaseDao<User, Long> implements User.Columns {
@Override
public int addProperties(List<Property> list, int offset) {
//Add columns here.
list.add(new Property(ID, Property.TYPE_INTEGER).setPrimaryKey(true));
list.add(new Property(NAME, Property.TYPE_TEXT));
list.add(new Property(AGE, Property.TYPE_INTEGER));
return 0;
}
@Override
public User createEntity() {
//Create entity instance.
return new User();
}
@Override
public int fillEntity(USER user, Cursor cursor, int offset) {
//Fill entity instance with data from cursor in the order of add columns to increase efficiency.
user.setId(cursor.getLong(offset++));
user.setName(cursor.getString(offset++));
user.setAge(cursor.getInt(offset++));
return offset;
}
@Override
public String getTableName() {
//Return table name.
return "User";
}
@Override
public ContentValues convertValues(User user) {
//Convert entity to content values for inserting or updating.
ContentValues values = new ContentValues();
putAutoIncrementID(values, user.getId());
values.put(NAME, user.getName());
values.put(AGE, user.getAge());
return values;
}
}
public class MyDBHelper extends BaseDBHelper {
@Override
public List<AbstractDao> getTables() {
//Return your daos here then we can create/update table for you.
ArrayList<AbstractDao> list = new ArrayList<>();
list.add(UserDao.getInstance());
list.add(EmployeeDao.getInstance());
return list;
}
...
}
new MyDBHelper(this, DB_NAME, null, DB_VERSION);
Random random = new Random(System.currentTimeMillis());
User user = new User();
user.setAge(random.nextInt(80));
user.setName("Irwin[" + random.nextInt(100) + "]");
UserDao.getInstance().insert(user);
Employee employee = new Employee();
employee.setAge(random.nextInt(60));
employee.setName("Jack[" + random.nextInt(100) + "]");
employee.setSalary(random.nextInt(50000));
employee.setPost(random.nextInt(8));
employee.setCode(String.valueOf(random.nextInt()));
employee.setScore(random.nextInt(100));
EmployeeDao.getInstance().insert(employee);
//We provided many convenient methods for using in common database development. See {@link com.irwin.database.AbstractDao} for more information.
User savedUser = (User) UserDao.getInstance().queryByID(1L);
Log.i(TAG, "Saved user:\n" + savedUser);
List<Employee> list = EmployeeDao.getInstance().queryAll();
4.We use DefaultUpgrader
to upgrade the database as default. You can choose StrictUpgrader
which will treat columns changes strictly, or implement your own upgrader and tell your Database helper:
new MyDBHelper(this, DB_NAME, null, DB_VERSION).setUpgrader(YOUR CHOOSEN/IMPLEMENTATION);
5.Implement your dao as ObservableDao or Decorate a dao with ObservableDaoDecor so you can subscribe events of table change by registerObserver(IDBObserver observer)
, Enjoy it.
Maybe we can add support for annotation-processing so we can generate daos according to entities like GreenDao.
Any advice will be appreciated:D
More information about DBFramework:http://www.cnblogs.com/oxgen/p/7161986.html
Email:[email protected]