sqlite 版本升级及数据库操作类(二)

数据库操作工具类(增删改查

/**
 * 保存数据到数据库
 */
public void savaShowData(String ctype, String title, String source) {
    synchronized (mySqlitehelper) {
        if (!writableDatabase.isOpen()) {
            writableDatabase = mySqlitehelper.getWritableDatabase();
        }
        writableDatabase.beginTransaction();
        try {
            ContentValues contentValues = new ContentValues();
            contentValues.put("ctype", ctype);
            contentValues.put("title", title);
            contentValues.put("source", source);
            writableDatabase.insert("NewsShow", null, contentValues);
            writableDatabase.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            writableDatabase.endTransaction();
            writableDatabase.close();
        }
    }
}
/**
 * 获取数据库缓存的前30条数据
 * @return
 */
public List getshowDate() {
    synchronized (mySqlitehelper) {
        if (!writableDatabase.isOpen()) {
            writableDatabase = mySqlitehelper.getWritableDatabase();
        }
        Cursor cursor = writableDatabase.query("NewsShow", null, null, null, null, null, null, null);
        Cursor queryCursor = null;
        try {
            if (cursor.getCount() > 0) {
                int limitNum;
                if (cursor.getCount() <= 30) limitNum = cursor.getCount();
                else limitNum = cursor.getCount() - (cursor.getCount() - 30);
                //select * from 表名 limit 0,10;
                String sql = "select * from NewsShow limit " + 0 + "," + limitNum;
                queryCursor = writableDatabase.rawQuery(sql, null);
                showList = new ArrayList<News>();
                while (queryCursor.moveToNext()) {
                    String ctype = queryCursor.getString(queryCursor.getColumnIndex("ctype"));
                    String title = queryCursor.getString(queryCursor.getColumnIndex("title"));
                    String source = queryCursor.getString(queryCursor.getColumnIndex("source"));
                    showList.add(new News(ctype, title, source, source_type, type, detail_a, listImages, data_id, false, isRead));
                }
                return showList;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != queryCursor)
                queryCursor.close();
            cursor.close();
        }
    }
    return showList;
}
/**
 * 删除数据库指定数据 (_id)
 * @param number
 */
public void deleteData(int number) {
    synchronized (mySqlitehelper) {
        if (!writableDatabase.isOpen()) {
            writableDatabase = mySqlitehelper.getWritableDatabase();
        }
        try {
            writableDatabase.delete("NewsShow", "_id=?", new String[]{String.valueOf(number)});
            writableDatabase.close();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(TAG, "delete fail" + e);
        } finally {
            writableDatabase.close();
        }
    }
}
/**
 * 清空数据库并重置id
 */
public void deleteShowData() {
    synchronized (mySqlitehelper) {
        if (!writableDatabase.isOpen()) {
            writableDatabase = mySqlitehelper.getWritableDatabase();
        }
        try {
            String sql = "DELETE FROM " + "NewsShow" + ";";
            writableDatabase.execSQL(sql);
            String sqlRevert = "update sqlite_sequence set seq=0 where name='" + "NewsShow" + "'";
            writableDatabase.execSQL(sqlRevert);
            writableDatabase.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            writableDatabase.close();
        }
    }
}

/**
 *  查询数据库是否已存在某条数据
 */
public String queryDate(String detail_a) {
    String url = null;
    synchronized (mySqlitehelper) {
        if (!writableDatabase.isOpen()) {
            writableDatabase = mySqlitehelper.getWritableDatabase();
        }
        String sql = "select detail_a from NewsShow where detail_a='" + detail_a + "'";
        Cursor cursor = writableDatabase.rawQuery(sql, null);
        try {
            while (cursor.moveToNext()) {
                url = cursor.getString(cursor.getColumnIndex("detail_a"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(TAG, " query savaSqlite is null:" + e);
        } finally {
            cursor.close();
            writableDatabase.close();
        }
    }
    return url;
}

/**
 * 更新数据库表中某条数据
 */
public void updataHasRead(String detialUrl) {
    if (!detialUrl.equals(queryDate(detialUrl))) {//先查询 再去更新已读
        return;
    }
    synchronized (mySqlitehelper) {
        if (!writableDatabase.isOpen()) {
            writableDatabase = mySqlitehelper.getWritableDatabase();
        }
        try {
            ContentValues values = new ContentValues();
            values.put("isRead", "Y");
            int isRead = writableDatabase.update("NewsShow", values, "detail_a=?", new String[]{detialUrl});
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(TAG, "updataHasRead :" + e);
        } finally {
            writableDatabase.close();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值