返回查询的Getter

时间:2015-07-07 22:43:43

标签: java android sqlite

我正在尝试做的是在SQL表类中创建一个getter方法。

我目前有这个(更喜欢它作为.query,但似乎无法解决这个问题):

public static Cursor getAllArchived(SQLiteDatabase readOnlyDatabase) {
        return readOnlyDatabase.rawQuery("SELECT * FROM rss_items WHERE is_archived = 1 AND id = ?", new String[] {});
}

我这样称呼它:

Cursor allArchved = RssItemTable.getAllArchived(databaseOpenHelper.getReadableDatabase());

预期的功能是我可以调用getter方法,它会为我提供当前存档的列表。

我是否这样做(使用Cursor并创建.rawQuery)?

编辑:

RssItemTable.class

public class RssItemTable extends Table {

    public static class Builder implements Table.Builder {

        ContentValues values = new ContentValues();

        public Builder setLink(String link) {
            values.put(COLUMN_LINK, link);
            return this;
        }

        public Builder setTitle (String title){
            values.put(COLUMN_TITLE, title);
            return this;
        }

        public Builder setDescription(String description){
            values.put(COLUMN_DESCRIPTION, description);
            return this;
        }

        public Builder setGUID(String guid) {
            values.put(COLUMN_GUID, guid);
            return this;
        }

        public Builder setPubDate(long pubDate) {
            values.put(COLUMN_PUB_DATE, pubDate);
            return this;
        }

        public Builder setEnclosure(String enclosure){
            values.put(COLUMN_ENCLOSURE, enclosure);
            return this;
        }

        public Builder setMIMEType(String mimeType){
            values.put(COLUMN_MIME_TYPE, mimeType);
            return this;
        }

        public Builder setRSSFeed(long rssFeed){
            values.put(COLUMN_RSS_FEED, rssFeed);
            return this;
        }

        @Override
        public long insert(SQLiteDatabase writableDB){
            return writableDB.insert(RssItemTable.NAME, null, values);
        }
    }

    public static String getLink(Cursor cursor){
        return getString(cursor, COLUMN_LINK);
    }

    public static String getTitle(Cursor cursor){
        return getString(cursor, COLUMN_TITLE);
    }

    public static String getDescription(Cursor cursor){
        return getString(cursor, COLUMN_DESCRIPTION);
    }

    public static String getGUID(Cursor cursor){
        return getString(cursor, COLUMN_GUID);
    }

    public static long getRssFeedId(Cursor cursor){
        return getLong(cursor, COLUMN_RSS_FEED);
    }

    public static long getPubDate(Cursor cursor){
        return getLong(cursor, COLUMN_PUB_DATE);
    }

    public static String getEnclosure(Cursor cursor){
        return getString(cursor, COLUMN_ENCLOSURE);
    }

    public static boolean getFavorite(Cursor cursor){
        return getBoolean(cursor, COLUMN_FAVORITE);
    }

    public static boolean getArchived(Cursor cursor){
        return getBoolean(cursor, COLUMN_ARCHIVED);
    }

    public static Cursor getAllArchived(SQLiteDatabase readOnlyDatabase) {
        return readOnlyDatabase.rawQuery("SELECT * FROM rss_items WHERE is_archived = 1 AND id = ?", new String[] {});
    }

    private static final String NAME =  "rss_items";
    private static final String COLUMN_LINK = "link";
    private static final String COLUMN_TITLE = "title";
    private static final String COLUMN_DESCRIPTION = "description";
    private static final String COLUMN_GUID = "guid";
    private static final String COLUMN_PUB_DATE = "pub_date";
    private static final String COLUMN_ENCLOSURE = "enclosure";
    private static final String COLUMN_MIME_TYPE = "mime_type";
    private static final String COLUMN_RSS_FEED = "rss_feed";
    private static final String COLUMN_FAVORITE = "is_favorite";
    private static final String COLUMN_ARCHIVED = "is_archived";

    @Override
    public String getName(){
        return "rss_items";
    }

    @Override
    public String getCreateStatement(){
        return "CREATE TABLE " + getName() + " ("
                + COLUMN_ID + " INTEGER PRIMARY KEY,"
                + COLUMN_LINK + " TEXT,"
                + COLUMN_TITLE + " TEXT,"
                + COLUMN_DESCRIPTION + " TEXT,"
                + COLUMN_GUID + " TEXT,"
                + COLUMN_PUB_DATE + " INTEGER,"
                + COLUMN_ENCLOSURE + " TEXT,"
                + COLUMN_MIME_TYPE + " TEXT,"
                + COLUMN_RSS_FEED + " INTEGER,"
                + COLUMN_FAVORITE + " INTEGER DEFAULT 0,"
                + COLUMN_ARCHIVED + " INTEGER DEFAULT 0)";
    }

}

2 个答案:

答案 0 :(得分:0)

试试这个:

Cursor c = readOnlyDatabase.rawQuery("SELECT * FROM rss_items WHERE is_archived = 1", null);

可以使用rawQuery。如果您知道要做什么,实际上是在保存构建器代码。在此之后你可以尝试:

if (c != null) {
    c.moveToFirst();
    List<RssItem> rssss = new ArrayList<RssItem>();
    for (int k = 0; k < c.getCount(); k++) {
        RssItem rss = new RssItem();
        // depending on the order of your columns.
        rss.name = cursor.getString(0);
        rss.xxxx = cursor.getInt(xxx);
        rssss.add(rss);
    }
    c.close();
}

答案 1 :(得分:0)

  

我这样做是对的(使用Cursor并创建.rawQuery)?

,但是您缺少一个值为id(参数selectionArgs)的值,例如,其中id值必须为12,然后是:

 return readOnlyDatabase.rawQuery("SELECT * FROM rss_items WHERE is_archived = 1 AND id = ?", new String[] {"12"});