package com.gh.gamecenter.db; import android.content.Context; import com.gh.gamecenter.db.info.DataCollectionInfo; import com.j256.ormlite.dao.Dao; import java.sql.SQLException; import java.util.List; public class DataCollectionDao { private DatabaseHelper helper; private Dao dao; public DataCollectionDao(Context context) { try { helper = DatabaseHelper.getHelper(context); dao = helper.getDao(DataCollectionInfo.class); } catch (SQLException | IllegalStateException e) { e.printStackTrace(); } } /** * 查找一个数据 */ public List findByType(String type) { try { return dao.queryForEq("type", type); } catch (SQLException | IllegalStateException e) { e.printStackTrace(); } return null; } /** * 添加一个数据 */ public void add(DataCollectionInfo entity) { try { dao.create(entity); } catch (SQLException | IllegalStateException e) { // java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: e.printStackTrace(); } } /** * 删除一个数据 */ public void delete(String id) { try { dao.deleteById(id); } catch (SQLException | IllegalStateException e) { // java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: e.printStackTrace(); } } /** * 删除一组数据 */ public void delete(List ids) { try { dao.deleteIds(ids); } catch (SQLException | IllegalStateException e) { e.printStackTrace(); } } /** * 根据id获取某一个数据 */ public DataCollectionInfo find(String id) { try { return dao.queryForId(id); } catch (SQLException | IllegalStateException e) { e.printStackTrace(); } return null; } /** * 获取所有的数据 */ public List getAll() { try { return dao.queryForAll(); } catch (SQLException | IllegalStateException e) { e.printStackTrace(); } return null; } /** * 获取点击数据 */ public List getClickData() { try { return dao.queryForEq("type", "click-item"); } catch (SQLException | IllegalStateException e) { e.printStackTrace(); } return null; } /** * 更新数据 */ public void update(DataCollectionInfo entity) { try { dao.update(entity); } catch (SQLException | IllegalStateException e) { e.printStackTrace(); } } }