private String dbPath = Environment.getExternalStorageDirectory() + "/database";
private final String DB_NAME = "test.db";
private DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
.setDbName(DB_NAME)
.setDbDir(new File(dbPath))
.setDbVersion(1)
.setDbOpenListener(new DbManager.DbOpenListener() {
@Override
public void onDbOpened(DbManager db) {
db.getDatabase().enableWriteAheadLogging();
}
})
.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
@Override
public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
}
});
private final DbManager db = x.getDb(daoConfig);
* 新增数据对象到数据库
*
* @param object
* @return
*/
public boolean add(Object object) {
try {
db.saveOrUpdate(object);
} catch (DbException e) {
e.printStackTrace();
return false;
}
return true;
}
* 绑定id形式的新增数据
*
* @param object
* @return
*/
public boolean saveBindId(Object object) {
try {
return db.saveBindingId(object);
} catch (DbException e) {
e.printStackTrace();
return false;
}
}
* 删除学生信息
*
* @param id 学生的学号
* @return 返回删除结果,true为成功,false为失败
*/
public boolean deleteChild(int id) {
try {
db.deleteById(Child.class, id);
} catch (DbException e) {
e.printStackTrace();
return false;
}
return true;
}
* 更新
*
* @param object 数据对象
* @return
*/
public boolean update(Object object) {
try {
db.saveOrUpdate(object);
return true;
} catch (DbException e) {
e.printStackTrace();
return false;
}
}
* 查询所有学生信息
*
* @return
*/
public List<Child> searchChild() {
try {
return db.findAll(Child.class);
} catch (DbException e) {
e.printStackTrace();
}
return null;
}
* 根据学号查询学生信息
*
* @param id 学生学号
* @return
*/
public Child searchChildById(int id) {
try {
return db.findById(Child.class, id);
} catch (DbException e) {
e.printStackTrace();
}
return null;
}
* 删除Child表
*
* @return
*/
public boolean dropChildTable() {
try {
db.dropTable(Child.class);
} catch (DbException e) {
e.printStackTrace();
return false;
}
return true;
}