Example usage for android.database.sqlite SQLiteDatabase query

List of usage examples for android.database.sqlite SQLiteDatabase query

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase query.

Prototype

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy,
        String having, String orderBy, String limit) 

Source Link

Document

Query the given table, returning a Cursor over the result set.

Usage

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * get the DB object representing remote file by its URL
 *
 * @param url remote file URL//from w w  w . j  a va 2 s  . c o m
 * @return remote file object from DB
 */
private RemoteFile getRemoteFile(String url) {
    if (!isDBAvailable())
        return null;

    RemoteFile rf = null;
    SQLiteDatabase db = getOpenHelper().getReadableDatabase();
    readLock(true);
    Cursor c = null;
    try {
        c = db.query(TABLE_REMOTEFILES, null, "url=?", new String[] { url }, null, null, null, null);
        if (c.moveToFirst())
            rf = handleRemoteFileCursor(c);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (c != null && !c.isClosed())
            c.close();
        readLock(false);
    }
    return rf;
}

From source file:org.ttrssreader.controllers.DBHelper.java

@SuppressLint("UseSparseArrays")
Map<Integer, String> getMarked(String mark, int status) {
    if (!isDBAvailable())
        return new HashMap<>();

    SQLiteDatabase db = getOpenHelper().getReadableDatabase();
    readLock(true);/*from w w  w  . ja  v a2s .c om*/
    Cursor c = null;
    try {
        c = db.query(TABLE_MARK, new String[] { "id", MARK_NOTE }, mark + "=" + status, null, null, null, null,
                null);

        Map<Integer, String> ret = new HashMap<>(c.getCount());
        while (c.moveToNext()) {
            ret.put(c.getInt(0), c.getString(1));
        }
        return ret;

    } finally {
        if (c != null && !c.isClosed())
            c.close();
        readLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

public ArrayList<Article> queryArticlesForImagecache() {
    if (!isDBAvailable())
        return null;

    SQLiteDatabase db = getOpenHelper().getReadableDatabase();
    readLock(true);/*from  www . j a v a 2 s  .  co m*/
    Cursor c = null;
    try {
        c = db.query(TABLE_ARTICLES, new String[] { "_id", "content", "attachments" },
                "cachedImages IS NULL AND isUnread>0", null, null, null, null, "1000");

        ArrayList<Article> ret = new ArrayList<>(c.getCount());
        while (c.moveToNext()) {
            Article a = new Article();
            a.id = c.getInt(0);
            a.content = c.getString(1);
            a.attachments = parseAttachments(c.getString(2));
            ret.add(a);
        }
        return ret;
    } finally {
        if (c != null && !c.isClosed())
            c.close();
        readLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

public int getUnreadCount(int id, boolean isCat) {
    if (!isDBAvailable())
        return 0;

    StringBuilder selection = new StringBuilder("isUnread>0");
    String[] selectionArgs = new String[] { String.valueOf(id) };

    if (isCat && id >= 0) {
        // real categories
        selection.append(" and feedId in (select _id from feeds where categoryId=?)");
    } else {//ww  w. j  a  v  a  2 s.c om
        if (id < 0) {
            // virtual categories
            switch (id) {
            // All Articles
            case Data.VCAT_ALL:
                selectionArgs = null;
                break;

            // Fresh Articles
            case Data.VCAT_FRESH:
                selection.append(" and updateDate>?");
                selectionArgs = new String[] { String
                        .valueOf(new Date().getTime() - Controller.getInstance().getFreshArticleMaxAge()) };
                break;

            // Published Articles
            case Data.VCAT_PUB:
                selection.append(" and isPublished>0");
                selectionArgs = null;
                break;

            // Starred Articles
            case Data.VCAT_STAR:
                selection.append(" and isStarred>0");
                selectionArgs = null;
                break;

            default:
                // Probably a label...
                selection.append(" and feedId=?");
            }
        } else {
            // feeds
            selection.append(" and feedId=?");
        }
    }

    // Read count for given feed
    int ret = 0;
    SQLiteDatabase db = getOpenHelper().getReadableDatabase();
    readLock(true);
    Cursor c = null;
    try {
        c = db.query(TABLE_ARTICLES, new String[] { "count(*)" }, selection.toString(), selectionArgs, null,
                null, null, null);

        if (c.moveToFirst())
            ret = c.getInt(0);
    } finally {
        if (c != null && !c.isClosed())
            c.close();
        readLock(false);
    }

    return ret;
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * Set unread counters for feeds and categories according to real amount of unread articles.
 *//*from   ww  w .  ja va  2 s  . c  o m*/
void calculateCounters() {
    if (!isDBAvailable())
        return;

    long time = System.currentTimeMillis();
    int total = 0;
    Cursor c = null;

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        ContentValues cv = new ContentValues(1);

        // First of all, reset all feeds and all categories to unread=0
        cv.put("unread", 0);
        db.update(TABLE_FEEDS, cv, null, null);
        db.update(TABLE_CATEGORIES, cv, null, null);

        // Count all feeds where unread articles exist
        try {
            // select feedId, count(*) from articles where isUnread>0 group by feedId
            c = db.query(TABLE_ARTICLES, new String[] { "feedId", "count(*)" }, "isUnread>0", null, "feedId",
                    null, null, null);

            // update feeds
            while (c.moveToNext()) {
                int feedId = c.getInt(0);
                int unreadCount = c.getInt(1);

                total += unreadCount;

                cv.put("unread", unreadCount);
                db.update(TABLE_FEEDS, cv, "_id=" + feedId, null);
            }
        } finally {
            if (c != null && !c.isClosed())
                c.close();
        }

        // Count all categories where feeds with unread articles exist
        try {
            // select categoryId, sum(unread) from feeds where categoryId >= 0 group by categoryId
            c = db.query(TABLE_FEEDS, new String[] { "categoryId", "sum(unread)" }, "categoryId>=0", null,
                    "categoryId", null, null, null);

            // update real categories
            while (c.moveToNext()) {
                int categoryId = c.getInt(0);
                int unreadCount = c.getInt(1);

                cv.put("unread", unreadCount);
                db.update(TABLE_CATEGORIES, cv, "_id=" + categoryId, null);
            }
        } finally {
            if (c != null && !c.isClosed())
                c.close();
        }

        // Count special categories
        cv.put("unread", total);
        db.update(TABLE_CATEGORIES, cv, "_id=" + Data.VCAT_ALL, null);

        cv.put("unread", getUnreadCount(Data.VCAT_FRESH, true));
        db.update(TABLE_CATEGORIES, cv, "_id=" + Data.VCAT_FRESH, null);

        cv.put("unread", getUnreadCount(Data.VCAT_PUB, true));
        db.update(TABLE_CATEGORIES, cv, "_id=" + Data.VCAT_PUB, null);

        cv.put("unread", getUnreadCount(Data.VCAT_STAR, true));
        db.update(TABLE_CATEGORIES, cv, "_id=" + Data.VCAT_STAR, null);

        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }

    Log.i(TAG, String.format("Fixed counters, total unread: %s (took %sms)", total,
            (System.currentTimeMillis() - time)));
}

From source file:com.rener.sea.DBHelper.java

public boolean isEmpty() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_USERS, new String[] { DBSchema.USER_ID }, null, null, null, null,
            null, null);//www. j  av a  2s  .  com
    boolean flag = cursor != null ? (cursor.getCount() > 0) : false;
    db.close();
    cursor.close();
    // TODO: tes it
    // return flag;
    return flag || !dummyDB;

}

From source file:com.rener.sea.DBHelper.java

public boolean isAgent(String username) {
    boolean agent = false;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_USERS, new String[] { DBSchema.USER_TYPE },
            DBSchema.USER_USERNAME + "=?", new String[] { String.valueOf(username) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();/*from w w  w  .  ja  v  a2s .  c  o  m*/
        agent = cursor.getString(0).equals("agent");
        db.close();
        cursor.close();
    }
    return agent;
}

From source file:com.rener.sea.DBHelper.java

public long getUserId(String username) {
    long agentId = -1;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_USERS, new String[] { DBSchema.USER_ID },
            DBSchema.USER_USERNAME + "=?", new String[] { String.valueOf(username) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();//from w  ww.j  av a 2 s  .c o  m
        agentId = cursor.getLong(0);
        db.close();
        cursor.close();
    }
    return agentId;
}

From source file:com.rener.sea.DBHelper.java

public Location findLocationById(long id) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_LOCATION, new String[] { DBSchema.LOCATION_ID },
            DBSchema.LOCATION_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();/*from   w  ww  .  j a  v a 2s. com*/
        Location location = new Location(cursor.getLong(0), this);
        db.close();
        cursor.close();
        return location;
    }
    return new Location(-1, this);
}

From source file:com.rener.sea.DBHelper.java

public List<Option> getAllOptions(long itemID) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_OPTION, new String[] { DBSchema.OPTION_ID },
            DBSchema.OPTION_PARENT_ID + "=?", new String[] { String.valueOf(itemID) }, null, null, null, null);
    ArrayList<Option> options = new ArrayList<>();
    if ((cursor != null) && (cursor.getCount() > 0)) {

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            options.add(new Option(cursor.getLong(0), this));
        }//  w w  w  .  j av a 2  s .  c  o  m

    }
    return options;
}