Created
January 8, 2016 10:19
-
-
Save jp1017/ea2553d6348dae92fa74 to your computer and use it in GitHub Desktop.
在Application实现得到DaoMaster和DaoSession的方法及数据库增删改查工具类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static DaoMaster daoMaster; | |
private static DaoSession daoSession; | |
/** | |
* 取得DaoMaster | |
* | |
* @param context | |
* @return | |
*/ | |
public static DaoMaster getDaoMaster(Context context) | |
{ | |
if (daoMaster == null) | |
{ | |
OpenHelper helper = new DaoMaster.DevOpenHelper(context, HBContant.DATABASE_NAME, null); | |
daoMaster = new DaoMaster(helper.getWritableDatabase()); | |
} | |
return daoMaster; | |
} | |
/** | |
* 取得DaoSession | |
* | |
* @param context | |
* @return | |
*/ | |
public static DaoSession getDaoSession(Context context) | |
{ | |
if (daoSession == null) | |
{ | |
if (daoMaster == null) | |
{ | |
daoMaster = getDaoMaster(context); | |
} | |
daoSession = daoMaster.newSession(); | |
} | |
return daoSession; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DBHelper | |
{ | |
private static Context mContext; | |
private static DBHelper instance; | |
private CityInfoDBDao cityInfoDao; | |
private DBHelper() | |
{ | |
} | |
public static DBHelper getInstance(Context context) | |
{ | |
if (instance == null) | |
{ | |
instance = new DBHelper(); | |
if (mContext == null) | |
{ | |
mContext = context; | |
} | |
// 数据库对象 | |
DaoSession daoSession = HBApplication.getDaoSession(mContext); | |
instance.cityInfoDao = daoSession.getCityInfoDBDao(); | |
} | |
return instance; | |
} | |
/** 添加数据 */ | |
public void addToCityInfoTable(CityInfo item) | |
{ | |
cityInfoDao.insert(item); | |
} | |
/** 查询 */ | |
public List<EstateLoveListJson> getCityInfoList() | |
{ | |
QueryBuilder<CityInfo> qb = cityInfoDao.queryBuilder(); | |
return qb.list(); | |
} | |
/** 查询 */ | |
public List<CityInfo> getCityInfo() | |
{ | |
return cityInfoDao.loadAll();// 查找图片相册 | |
} | |
/** 查询 */ | |
public boolean isSaved(int Id) | |
{ | |
QueryBuilder<CityInfo> qb = cityInfoDao.queryBuilder(); | |
qb.where(Properties.Id.eq(Id)); | |
qb.buildCount().count(); | |
return qb.buildCount().count() > 0 ? true : false;// 查找收藏表 | |
} | |
/** 删除 */ | |
public void deleteCityInfoList(int Id) | |
{ | |
QueryBuilder<CityInfo> qb = cityInfoDao.queryBuilder(); | |
DeleteQuery<CityInfo> bd = qb.where(Properties.Id.eq(Id)).buildDelete(); | |
bd.executeDeleteWithoutDetachingEntities(); | |
} | |
/** 删除 */ | |
public void clearCityInfo() | |
{ | |
cityInfoDao.deleteAll(); | |
} | |
/** 通过城市id查找其类型id */ | |
public int getTypeId(int cityId) | |
{ | |
QueryBuilder<CityInfo> qb = cityInfoDao.queryBuilder(); | |
qb.where(Properties.Id.eq(cityId)); | |
if (qb.list().size() > 0) | |
{ | |
return qb.list().get(0).getTypeId(); | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
/** 多重查询 */ | |
public List<CityInfo> getIphRegionList(int cityId) | |
{ | |
QueryBuilder<CityInfoDB> qb = cityInfoDao.queryBuilder(); | |
qb.where(qb.and(Properties.CityId.eq(cityId), Properties.InfoType.eq(HBContant.CITYINFO_IR))); | |
qb.orderAsc(Properties.Id);// 排序依据 | |
return qb.list(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment