Example usage for android.database Cursor getCount

List of usage examples for android.database Cursor getCount

Introduction

In this page you can find the example usage for android.database Cursor getCount.

Prototype

int getCount();

Source Link

Document

Returns the numbers of rows in the cursor.

Usage

From source file:com.android.unit_tests.DbSSLSessionCacheTest.java

@LargeTest
public void testExpire() throws Exception {
    DatabaseHelper helper = new DatabaseHelper(getContext());
    // clean up//w  ww.ja v a  2  s  . c  o  m
    DbSSLSessionCache cache = new DbSSLSessionCache(helper);
    cache.clear();

    long t0 = System.currentTimeMillis();
    for (int i = 0; i < DbSSLSessionCache.MAX_CACHE_SIZE + 2; i++) {
        final int port = i;
        cache.putSessionData(new MockSession() {

            public String getPeerHost() {
                return "test.host.com";
            }

            public int getPeerPort() {
                return port;
            }
        }, new byte[256]);
    }
    long t1 = System.currentTimeMillis();

    System.err.println("Time to insert " + (DbSSLSessionCache.MAX_CACHE_SIZE + 2) + " " + (t1 - t0));

    // first entry should have port 1.
    Cursor query = helper.getReadableDatabase().query(DbSSLSessionCache.SSL_CACHE_TABLE,
            new String[] { "hostport", "session" }, null, null, null, null, null);

    int cnt = query.getCount();

    assertTrue(query.moveToFirst()); // one row inserted
    String hostPort = query.getString(0);
    assertEquals("test.host.com:2", hostPort);
    while (query.moveToNext()) {
        hostPort = query.getString(0);
        String session = query.getString(1);
    }
    long t2 = System.currentTimeMillis();
    System.err.println("Time to load " + cnt + " " + (t2 - t1));

    query.close();
}

From source file:com.yuntongxun.ecdemo.storage.IMessageSqlManager.java

public static synchronized long isReadMsg(String msgId) {
    long count = 0;
    String sql = "select * from " + DatabaseHelper.TABLES_NAME_IM_MESSAGE + " where msgid = '" + msgId + "'";
    Cursor cursor = getInstance().sqliteDB().rawQuery(sql, null);
    if (cursor != null && cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            count = cursor.getInt(cursor.getColumnIndex("readcount"));
            cursor.close();//from w w w .  ja v a 2 s.  c  o m
            cursor = null;
        }
    }
    return count;
}

From source file:com.yuntongxun.ecdemo.storage.IMessageSqlManager.java

public static String getMsgReadStatus(String msgId) {
    String sql = "select * from " + DatabaseHelper.TABLES_NAME_IM_MESSAGE + " where msgid = '" + msgId + "'";
    Cursor cursor = getInstance().sqliteDB().rawQuery(sql, null);
    if (cursor != null && cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            String text = cursor.getString(cursor.getColumnIndex("isRead"));
            cursor.close();//  ww w .  j av  a2  s.  c om
            cursor = null;
            return text;
        }
    }
    return "0";
}

From source file:com.yuntongxun.ecdemo.storage.IMessageSqlManager.java

/**
 * @return//w w  w  .j a va 2s. com
 */
public static int getMaxVersion() {
    String sql = "select max(version) as maxVersion from " + DatabaseHelper.TABLES_NAME_IM_MESSAGE;
    Cursor cursor = getInstance().sqliteDB().rawQuery(sql, null);
    if (cursor != null && cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            int maxVersion = cursor.getInt(cursor.getColumnIndex("maxVersion"));
            cursor.close();
            ;
            return maxVersion;
        }
    }
    return 0;
}

From source file:com.yuntongxun.ecdemo.storage.IMessageSqlManager.java

/**
 * ????/*w w w.  j av  a 2s . com*/
 *
 * @return
 */
public static boolean isFireMsg(String msgId) {
    String sql = "select * from " + DatabaseHelper.TABLES_NAME_IM_MESSAGE + " where msgid = '" + msgId + "'";
    Cursor cursor = getInstance().sqliteDB().rawQuery(sql, null);
    if (cursor != null && cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            String text = cursor.getString(cursor.getColumnIndex("text"));
            cursor.close();
            cursor = null;
            return "fireMessage".equals(text);
        }
    }
    return false;
}

From source file:com.nextgis.firereporter.ReporterService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Received start id " + startId + ": " + intent);
    super.onStartCommand(intent, flags, startId);

    if (intent == null)
        return START_STICKY;
    String action = intent.getAction();
    if (action.equals(ACTION_STOP)) {
        this.stopSelf();
    } else if (action.equals(ACTION_START)) {
        Log.d(TAG, "Action " + ACTION_START);
        Context c = this.getApplicationContext();
        SharedPreferences prefs = getSharedPreferences(MainActivity.PREFERENCES,
                MODE_PRIVATE | MODE_MULTI_PROCESS);
        long nMinTimeBetweenSend = prefs.getLong(SettingsActivity.KEY_PREF_INTERVAL + "_long",
                DateUtils.MINUTE_IN_MILLIS);
        boolean bBattEconomy = prefs.getBoolean(SettingsActivity.KEY_PREF_SERVICE_BATT_SAVE, true);

        if (!HttpGetter.IsNetworkAvailible(c)) {
            ScheduleNextUpdate(c, nMinTimeBetweenSend, bBattEconomy);
            Log.d(TAG, "network not availible");
            return START_NOT_STICKY;
        }/*from  w  w w. ja  v a  2s  .  c  om*/

        Cursor cursor = ReportsDB.query(ReportsDatabase.TABLE_POS, null, null, null, null, null, null);
        Log.d(TAG, "record count = " + cursor.getCount());
        if (cursor.getCount() < 1) {
            this.stopSelf();
            return START_NOT_STICKY;
        }

        if (fireDataSender == null) {
            Log.d(TAG, "new fireDataSender");
            fireDataSender = new SendFireDataTask();
            fireDataSender.execute(this.getApplicationContext());
            ScheduleNextUpdate(c, nMinTimeBetweenSend, bBattEconomy);
        } else if (fireDataSender.getStatus() == AsyncTask.Status.FINISHED) {
            Log.d(TAG, "exist fireDataSender");
            fireDataSender.execute(this.getApplicationContext());
            ScheduleNextUpdate(c, nMinTimeBetweenSend, bBattEconomy);
        } else if (fireDataSender.getStatus() == AsyncTask.Status.PENDING
                || fireDataSender.getStatus() == AsyncTask.Status.RUNNING) {
            Log.d(TAG, "exist fireDataSender executing");
            ScheduleNextUpdate(c, nMinTimeBetweenSend, bBattEconomy);
        } else {
            Log.d(TAG, "unexpected behaviour");
            this.stopSelf();
        }
    }
    return START_NOT_STICKY;
}

From source file:com.money.manager.ex.datalayer.StockRepository.java

/**
 * Retrieves all record ids which refer the given symbol.
 * @return array of ids of records which contain the symbol.
 *///  w  w w  .ja va2s .com
public int[] findIdsBySymbol(String symbol) {
    int[] result;

    Cursor cursor = getContext().getContentResolver().query(this.getUri(), new String[] { StockFields.STOCKID },
            StockFields.SYMBOL + "=?", new String[] { symbol }, null);
    if (cursor == null)
        return null;

    int records = cursor.getCount();
    result = new int[records];

    for (int i = 0; i < records; i++) {
        cursor.moveToNext();
        result[i] = cursor.getInt(cursor.getColumnIndex(StockFields.STOCKID));
    }
    cursor.close();

    return result;
}

From source file:com.caju.uheer.app.services.infrastructure.ContactablesLoaderCallbacks.java

@Override
public Loader<Cursor> onCreateLoader(int loaderIndex, Bundle args) {
    String query = "";
    JSONArray nearbyUsers = new JSONArray();

    Uri uri = Uri.withAppendedPath(CommonDataKinds.Contactables.CONTENT_FILTER_URI, query);

    // Sort results such that rows for the same contact stay together.
    String sortBy = CommonDataKinds.Contactables.LOOKUP_KEY;

    CursorLoader cursorLoader = new CursorLoader(mContext, // Context
            uri, // URI representing the table/resource to be queried
            null, // projection - the list of columns to return.  Null means "all"
            null, // selection - Which rows to return (condition rows must match)
            null, // selection args - can be provided separately and subbed into selection.
            sortBy); // string specifying sort order

    try {//from   w  w w .  j  a v a  2 s  .  c om
        // Parsing the JSON received from server.
        String nearbyUsersString = args.getString("jsonString");
        nearbyUsers = new JSONObject(nearbyUsersString).getJSONArray("nearbyUsers");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < nearbyUsers.length(); i++) {
        //            Log.d("json", "" + nearbyUsers.getJSONObject(0).get("email"));
        try {
            query = nearbyUsers.getJSONObject(i).getString("email");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //            query = args.getString("query");

        uri = Uri.withAppendedPath(CommonDataKinds.Contactables.CONTENT_FILTER_URI, query);

        cursorLoader = new CursorLoader(mContext, // Context
                uri, // URI representing the table/resource to be queried
                null, // projection - the list of columns to return.  Null means "all"
                null, // selection - Which rows to return (condition rows must match)
                null, // selection args - can be provided separately and subbed into selection.
                sortBy); // string specifying sort order

        Cursor cursor = cursorLoader.loadInBackground();

        Toast.makeText(mContext, "entrei no cursorLoader", Toast.LENGTH_LONG).show();
        if (cursor.getCount() == 0) {
            return cursorLoader;
        }

        int nameColumnIndex = cursor.getColumnIndex(CommonDataKinds.Contactables.DISPLAY_NAME);
        int lookupColumnIndex = cursor.getColumnIndex(CommonDataKinds.Contactables.LOOKUP_KEY);

        cursor.moveToFirst();

        String lookupKey = "";
        String displayName = "";
        do {
            String currentLookupKey = cursor.getString(lookupColumnIndex);
            if (!lookupKey.equals(currentLookupKey)) {
                displayName = cursor.getString(nameColumnIndex);
                lookupKey = currentLookupKey;
            }
        } while (cursor.moveToNext());

        Toast.makeText(mContext, displayName, Toast.LENGTH_LONG).show();
        try {
            JSONObject jobj = new JSONObject().put("name", displayName);
            if (!nearbyUsers.getJSONObject(i).has("channel")) {
                double[] geoPoint = new double[2];
                geoPoint[0] = nearbyUsers.getJSONObject(i).getJSONObject("geoPoint").getDouble("latitude");
                geoPoint[1] = nearbyUsers.getJSONObject(i).getJSONObject("geoPoint").getDouble("longitude");
                jobj.put("lat", geoPoint[0]);
                jobj.put("lon", geoPoint[1]);
            } else {
                String channel = nearbyUsers.getJSONObject(i).getString("channel");
                jobj.put("channel", channel);
            }
            usersFound.put(jobj);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return cursorLoader;
}

From source file:com.seneca.android.senfitbeta.DbHelper.java

public boolean checkEmailExists(String email) {
    String query = "Select * from " + USER_TABLE + " where email like '" + email + "'";

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    cursor.moveToFirst();/*w w w.  java  2 s.c om*/

    if (cursor.getCount() > 0) {
        return true;
    } else {
        return false;
    }
}

From source file:com.yuntongxun.ecdemo.storage.IMessageSqlManager.java

public static ECMessage getLastECMessage() {
    int maxVersion = getMaxVersion();
    String sql = "select im_message.* ,im_thread.sessionId from im_message ,im_thread where version = "
            + maxVersion + " and im_message.sid=im_thread.id";
    Cursor cursor = getInstance().sqliteDB().rawQuery(sql, null);
    if (cursor != null && cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            ECMessage ecMessage = packageMessage(cursor);
            String sessionid = cursor.getString(cursor.getColumnIndexOrThrow(IThreadColumn.THREAD_ID));
            ecMessage.setSessionId(sessionid);
            cursor.close();//from w w  w .  j  a  v a2 s  .c  om
            if (ecMessage != null) {
                return ecMessage;
            }
        }
    }
    return null;
}