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) 

Source Link

Document

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

Usage

From source file:net.potterpcs.recipebook.RecipeData.java

public Cursor getRecipeIngredients(long rid) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return db.query(INGREDIENTS_TABLE, INGREDIENTS_FIELDS, IT_RECIPE_ID + " = ?",
                new String[] { Long.toString(rid) }, null, null, null);
    }//from ww w.j a v a 2  s.com
}

From source file:net.potterpcs.recipebook.RecipeData.java

public Cursor getRecipeDirections(long rid) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return db.query(DIRECTIONS_TABLE, DIRECTIONS_FIELDS, DT_RECIPE_ID + " = ?",
                new String[] { Long.toString(rid) }, null, null, DT_SEQUENCE);
    }//  w ww.  j a  v a 2  s.  c  om
}

From source file:net.potterpcs.recipebook.RecipeData.java

public Cursor getRecipeTags(long rid) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return db.query(TAGS_TABLE, TAGS_FIELDS, TT_RECIPE_ID + " = ?", new String[] { Long.toString(rid) },
                null, null, null);//  ww  w.ja  v a 2  s  .c  o m
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public Cursor getMatchingRecipesByTime(int max, int min, String sortBy) {
    String[] times = { Integer.toString(max), Integer.toString(min) };
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return db.query(RECIPES_TABLE, RECIPES_FIELDS, createTimeComparisonPattern(), times, null, null,
                sortBy);//w  w w  .j a  v  a 2s  .  c o m
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public Cursor getRecipesByTag(String tag, String sortBy) {
    String[] fields = RECIPES_FIELDS;
    fields[0] = RECIPES_TABLE + "." + RT_ID;
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return db.query(createInnerJoin(RECIPES_TABLE, TAGS_TABLE, RT_ID, TT_RECIPE_ID), fields,
                TT_TAG + " = ?", new String[] { tag }, null, null, sortBy);
    }/*from  w w  w.j a  va 2  s  . com*/
}

From source file:net.potterpcs.recipebook.RecipeData.java

public long getNumberOfRecipes() {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c = db.query(RECIPES_TABLE, new String[] { "_id" }, null, null, null, null, null);
        long num = c.getCount();
        c.close();/*w w w . j  a va  2 s. c o m*/
        return num;
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public boolean recipeHasTag(long rid, String tag) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c = db.query(TAGS_TABLE, TAGS_FIELDS, "(" + TT_TAG + " = ?) and (" + TT_RECIPE_ID + " = ?)",
                new String[] { tag, Long.toString(rid) }, null, null, null);
        boolean hasTag = c.getCount() != 0;
        c.close();/*from  ww w.j av  a  2 s . c o m*/
        return hasTag;
    }
}

From source file:com.jefftharris.passwdsafe.NotificationMgr.java

/** Load the expiration entries from the database */
private void loadEntries(SQLiteDatabase db) throws SQLException {
    long expiration;
    if (itsExpiryFilter != null) {
        expiration = itsExpiryFilter.getExpiryFromNow(null);
    } else {/*from w  w w.  j a  v  a2s.  co  m*/
        expiration = Long.MIN_VALUE;
    }
    LongReference nextExpiration = new LongReference(Long.MAX_VALUE);

    itsNotifUris.clear();
    HashSet<Long> uris = new HashSet<>();
    ArrayList<Long> removeUriIds = new ArrayList<>();
    Cursor uriCursor = db.query(DB_TABLE_URIS, new String[] { DB_COL_URIS_ID, DB_COL_URIS_URI }, null, null,
            null, null, null);
    try {
        while (uriCursor.moveToNext()) {
            long id = uriCursor.getLong(0);
            Uri uri = Uri.parse(uriCursor.getString(1));
            boolean exists = loadUri(id, uri, uris, expiration, nextExpiration, db);
            if (!exists) {
                removeUriIds.add(id);
            }
        }
    } finally {
        uriCursor.close();
    }

    Iterator<HashMap.Entry<Long, UriNotifInfo>> iter = itsUriNotifs.entrySet().iterator();
    while (iter.hasNext()) {
        HashMap.Entry<Long, UriNotifInfo> entry = iter.next();
        if (!uris.contains(entry.getKey())) {
            itsNotifyMgr.cancel(entry.getValue().getNotifId());
            iter.remove();
        }
    }

    for (Long removeId : removeUriIds) {
        removeUri(removeId, db);
    }

    PasswdSafeUtil.dbginfo(TAG, "nextExpiration: %tc", nextExpiration.itsValue);

    if ((nextExpiration.itsValue != Long.MAX_VALUE) && (itsExpiryFilter != null)) {
        if (itsTimerIntent == null) {
            Intent intent = new Intent(PasswdSafeApp.EXPIRATION_TIMEOUT_INTENT);
            itsTimerIntent = PendingIntent.getBroadcast(itsCtx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        }
        long nextTimer = System.currentTimeMillis() + (nextExpiration.itsValue - expiration);
        PasswdSafeUtil.dbginfo(TAG, "nextTimer: %tc", nextTimer);
        itsAlarmMgr.set(AlarmManager.RTC, nextTimer, itsTimerIntent);
    } else if (itsTimerIntent != null) {
        PasswdSafeUtil.dbginfo(TAG, "cancel expiration timer");
        itsAlarmMgr.cancel(itsTimerIntent);
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public String findCacheEntry(String uri) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c = db.query(CACHE_TABLE, CACHE_FIELDS, CT_URI + " = ?", new String[] { uri }, null, null, null);
        if (c.getCount() > 0) {
            c.moveToFirst();/*from w  w  w .  j av  a 2 s .  c  om*/
            String s = c.getString(c.getColumnIndex(CT_CACHED));
            c.close();
            return s;
        } else {
            c.close();
            return null;
        }
    }
}

From source file:com.test.onesignal.GenerateNotificationRunner.java

@Test
public void shouldHandleBasicNotifications() throws Exception {
    // Make sure the notification got posted and the content is correct.
    Bundle bundle = getBaseNotifBundle();
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    Assert.assertEquals(notifMessage, ShadowRoboNotificationManager.getLastShadowNotif().getContentText());
    Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);

    // Should have 1 DB record with the correct time stamp
    SQLiteDatabase readableDb = OneSignalDbHelper.getInstance(blankActivity).getReadableDatabase();
    Cursor cursor = readableDb.query(NotificationTable.TABLE_NAME, new String[] { "created_time" }, null, null,
            null, null, null);/*from w  w  w . j  a  v  a2  s.co m*/
    Assert.assertEquals(1, cursor.getCount());
    // Time stamp should be set and within a small range.
    long currentTime = System.currentTimeMillis() / 1000;
    cursor.moveToFirst();
    Assert.assertTrue(cursor.getLong(0) > currentTime - 2 && cursor.getLong(0) <= currentTime);
    cursor.close();

    // Should get marked as opened.
    NotificationOpenedProcessor_processFromContext(blankActivity, createOpenIntent(bundle));
    cursor = readableDb.query(NotificationTable.TABLE_NAME,
            new String[] { "opened", "android_notification_id" }, null, null, null, null, null);
    cursor.moveToFirst();
    Assert.assertEquals(1, cursor.getInt(0));
    Assert.assertEquals(0, ShadowBadgeCountUpdater.lastCount);
    cursor.close();

    // Should not display a duplicate notification, count should still be 1
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    readableDb = OneSignalDbHelper.getInstance(blankActivity).getReadableDatabase();
    cursor = readableDb.query(NotificationTable.TABLE_NAME, null, null, null, null, null, null);
    Assert.assertEquals(1, cursor.getCount());
    Assert.assertEquals(0, ShadowBadgeCountUpdater.lastCount);
    cursor.close();

    // Display a second notification
    bundle = getBaseNotifBundle("UUID2");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);

    // Go forward 4 weeks
    // Note: Does not effect the SQL function strftime
    ShadowSystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 2419201L * 1000L);

    // Display a 3rd notification
    // Should of been added for a total of 2 records now.
    // First opened should of been cleaned up, 1 week old non opened notification should stay, and one new record.
    bundle = getBaseNotifBundle("UUID3");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    readableDb = OneSignalDbHelper.getInstance(blankActivity).getReadableDatabase();
    cursor = readableDb.query(NotificationTable.TABLE_NAME,
            new String[] { "android_notification_id", "created_time" }, null, null, null, null, null);

    Assert.assertEquals(1, cursor.getCount());
    Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);

    cursor.close();
}