/**
* Created by yechaoa on 2018/10/29.
* Describe :
public class OrderDaoManager {
private static final String DB_NAME = "order_db";
private Context context;
@SuppressLint("StaticFieldLeak")
private volatile static OrderDaoManager manager = new OrderDaoManager();
private static DaoMaster sDaoMaster;
@SuppressLint("StaticFieldLeak")
private static DaoMaster.DevOpenHelper sHelper;
private static DaoSession sDaoSession;
* 单例模式获得操作数据库对象
public static OrderDaoManager getInstance() {
return manager;
public void init(Context context) {
this.context = context;
* 判断是否有存在数据库,如果没有则创建
private DaoMaster getDaoMaster() {
if (sDaoMaster == null) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
sDaoMaster = new DaoMaster(helper.getWritableDatabase());
return sDaoMaster;
* 完成对数据库的添加、删除、修改、查询操作
public DaoSession getDaoSession() {
if (sDaoSession == null) {
if (sDaoMaster == null) {
sDaoMaster = getDaoMaster();
sDaoSession = sDaoMaster.newSession();
return sDaoSession;
* 关闭所有的操作,数据库开启后,使用完毕要关闭
public void closeConnection() {
closeHelper();
closeDaoSession();
private void closeHelper() {
if (sHelper != null) {
sHelper.close();
sHelper = null;
private void closeDaoSession() {
if (sDaoSession != null) {
sDaoSession.clear();
sDaoSession = null;
}
编写OrderDBHelper类,处理增删改查
/**
* Created by yechao on 2018/10/29.
* Describe :
public class OrderDBHelper {
private OrderDaoManager mManager;
public OrderDBHelper(Context context) {
mManager = OrderDaoManager.getInstance();
mManager.init(context);
* 插入单条数据,如果表未创建,先创建OrderDB表
public boolean insertOrderDB(OrderDB o) {
boolean flag;
flag = mManager.getDaoSession().getOrderDBDao().insert(o) != -1;
return flag;
* 插入多条数据,在子线程操作
public boolean insertMultiOrderDB(final List<OrderDB> oList) {
boolean flag = false;
try {
mManager.getDaoSession().runInTx(new Runnable() {
@Override
public void run() {
for (OrderDB o : oList) {
mManager.getDaoSession().insertOrReplace(o);
flag = true;
} catch (Exception e) {
e.printStackTrace();
return flag;
* 修改一条数据
public boolean updateOrderDB(OrderDB o) {
boolean flag = false;
try {
mManager.getDaoSession().update(o);
flag = true;
} catch (Exception e) {
e.printStackTrace();
return flag;
* 删除单条记录
public boolean deleteOrderDB(OrderDB o) {
boolean flag = false;
try {
mManager.getDaoSession().delete(o);
flag = true;
} catch (Exception e) {
e.printStackTrace();
return flag;
* 删除所有记录
public boolean deleteAll() {
boolean flag = false;
try {
mManager.getDaoSession().deleteAll(OrderDB.class);
flag = true;
} catch (Exception e) {
e.printStackTrace();
return flag;
* 查询所有记录
public List<OrderDB> queryAllOrderDB() {
return mManager.getDaoSession().loadAll(OrderDB.class);
* 根据主键id查询记录
public OrderDB queryOrderDBById(long key) {
return mManager.getDaoSession().load(OrderDB.class, key);
* 使用native sql进行查询操作
public List<OrderDB> queryOrderDBByNativeSql(String sql, String[] conditions) {
return mManager.getDaoSession().queryRaw(OrderDB.class, sql, conditions);
* 使用queryBuilder进行查询
public List<OrderDB> queryOrderDBByQueryBuilder(long id) {
QueryBuilder<OrderDB> queryBuilder = mManager.getDaoSession().queryBuilder(OrderDB.class);
return queryBuilder.where(OrderDBDao.Properties.Id.eq(id)).list();
* orderAsc:升序排序 orderDesc: 降序排序
public List<OrderDB> queryOrderDBByQueryBuilder2(Integer price, String name) {
QueryBuilder<OrderDB> queryBuilder = mManager.getDaoSession().queryBuilder(OrderDB.class);
return queryBuilder
.where(OrderDBDao.Properties.Price.eq(price),
OrderDBDao.Properties.Name.eq(name))
.orderAsc(OrderDBDao.Properties.StatusId)
.list();
* 模糊查找 关键词需要前后加上%
public List<OrderDB> queryOrderDBByQueryBuilderSearch(String key) {
QueryBuilder<OrderDB> queryBuilder = mManager.getDaoSession().queryBuilder(OrderDB.class);
return queryBuilder
.where(OrderDBDao.Properties.Name.like("%" + key + "%"))
.orderAsc(OrderDBDao.Properties.StatusId)
.list();
}
使用
先初始化help对象
mOrderDBHelper = new OrderDBHelper(PickActivity.this);
插入多条
mOrderDBS = new ArrayList<>();
mOrderDBHelper = new OrderDBHelper(this);
for (int i = 0; i < mList.size(); i++) {
mOrderDBS.add(new OrderDB(null, mDate,mList.get(i).price,, mDate,mList.get(i).name, i, ...));
mOrderDBHelper.insertMultiOrderDB(mOrderDBS);
查询单条
Long id = mOrderDBS.get(pos).getId();
OrderDB orderDB1 = mOrderDBHelper.queryOrderDBById(id);
Long id = mOrderDBS.get(pos).getId();
OrderDB orderDB1 = mOrderDBHelper.queryOrderDBById(id);
orderDB1.setName("测试");
mOrderDBHelper.updateOrderDB(orderDB
public class DBHelper extends DaoMaster.OpenHelper {
public static final String DBNAME = "lenve.db";
public DBHelper(Context context) {
super(context, DBNAME, null);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
}
在onUpgrade方法中处理
然后数据库的初始化也需要重新处理
DBHelper devOpenHelper = new DBHelper(this);
DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
DaoSession daoSession = daoMaster.newSession();
userDao = daoSession.getUserDao();