我是否使用正确的方法从数据库中读取数据

时间:2013-11-25 05:53:56

标签: android gridview android-sqlite sqliteopenhelper

我有一个新闻应用程序,该应用程序有一个自定义gridView,其中包含appTitle,appNewNewsCount。

在我的数据库表中,我有一个名称是新的列,无论何时向表中添加新字段,其值都变为1,每当用户点击新闻时,该值都会更改为0.

在我的有gridView的活动中,我使用了这个指向我的SQLiteOpenHelper数据库类的函数:

我使用这段代码调用函数:

Toast.makeText(getApplicationContext(), String.valueOf(getNumberofNews("Political")), Toast.LENGTH_LONG).show();

函数的代码是:

private int getNumberofNews(String categoryRequested) {
        DB_New_Handler obj = null;
        try{
            obj = new DB_News_Handler(getApplicationContext());
            int newNews = obj.getNumberOfNewNews(categoryRequested);
            if (newNews > 0) {
                return newRings;
            }
        }catch (Exception ex){
            Log.d("getNumberofNews: ", ex.toString());
        }finally{
            if (obj != null){
                obj.close();
            }
        }
        return 0;
    }

这是我在SQLiteOpenHelper中使用的函数来收集新消息的数量:

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=?",
                new String[] { categoryName }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
        return 0;
    }

,现在的问题是getNumberOfNewNews函数。当假设返回0时,它总是返回1.

2 个答案:

答案 0 :(得分:3)

在查询新闻计数后立即编写数据库更新语句。您的更新应将KEY_NEW_ADDED字段更改为0.我已在您应该编写更新查询的方法中添加注释。

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=?",
                new String[] { categoryName }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
        return 0;
    }

答案 1 :(得分:0)

阿拉法特是对的 另外,您必须使用两个选择参数查询数据库...例如

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=? AND ",
                KEY_NEW_ADDED + "=? ",new String[] { categoryName, "1" }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }