Example usage for android.database Cursor getInt

List of usage examples for android.database Cursor getInt

Introduction

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

Prototype

int getInt(int columnIndex);

Source Link

Document

Returns the value of the requested column as an int.

Usage

From source file:com.sourceallies.android.zonebeacon.data.SQLiteQueryTest.java

private int getTableCount(String table) {
    Cursor cursor = source.getDatabase().rawQuery("SELECT count(*) FROM " + table, null);
    if (cursor != null && cursor.moveToFirst()) {
        int num = cursor.getInt(0);
        cursor.close();//www.  ja  v a2 s.c o m

        return num;
    }

    throw new RuntimeException("Error finding table count");
}

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

public static int getUnNotifyUnreadCount() {
    String sql = "SELECT sum(" + IThreadColumn.UNREAD_COUNT + ") FROM im_thread  \n"
            + "                         inner JOIN groups2 ON im_thread.sessionId = groups2.groupid and isnotice == 2";
    int count = 0;
    Cursor cursor = null;
    try {//ww w  .j ava2s  . c o m
        cursor = getInstance().sqliteDB().rawQuery(sql, null);
        if (cursor != null && cursor.getCount() > 0) {
            if (cursor.moveToFirst()) {
                count = cursor.getInt(cursor.getColumnIndex("sum(" + IThreadColumn.UNREAD_COUNT + ")"));
            }
        }
    } catch (Exception e) {
        LogUtil.e(TAG + " " + e.toString());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return count;
}

From source file:com.xorcode.andtweet.FriendTimeline.java

/**
 * Remove old records to ensure that the database does not grow too large.
 * Maximum number of records is configured in "history_size" preference
 * //  w  w  w .  ja va 2 s. com
 * @return Number of deleted records
 */
public int pruneOldRecords() {
    int nDeleted = 0;
    int nDeletedTime = 0;
    // We're using global preferences here
    SharedPreferences sp = MyPreferences.getDefaultSharedPreferences();
    int maxDays = Integer.parseInt(sp.getString(MyPreferences.KEY_HISTORY_TIME, "3"));
    long sinceTimestamp = 0;
    if (maxDays > 0) {
        sinceTimestamp = System.currentTimeMillis() - maxDays * (1000L * 60 * 60 * 24);

        SelectionAndArgs sa = new SelectionAndArgs();
        sa.addSelection(AndTweetDatabase.Tweets.SENT_DATE + " <  ?",
                new String[] { String.valueOf(sinceTimestamp) });

        if (mTimelineType != AndTweetDatabase.Tweets.TIMELINE_TYPE_MESSAGES) {
            // Don't delete Favorites!
            sa.addSelection(AndTweetDatabase.Tweets.FAVORITED + " = ?", new String[] { "0" });
        }
        nDeletedTime = mContentResolver.delete(mContentUri, sa.selection, sa.selectionArgs);
    }

    int nTweets = 0;
    int nToDeleteSize = 0;
    int nDeletedSize = 0;
    int maxSize = Integer.parseInt(sp.getString(MyPreferences.KEY_HISTORY_SIZE, "2000"));
    long sinceTimestampSize = 0;
    if (maxSize > 0) {
        try {

            nDeletedSize = 0;
            Cursor cursor = mContentResolver.query(mContentCountUri, null, null, null, null);
            if (cursor.moveToFirst()) {
                // Count is in the first column
                nTweets = cursor.getInt(0);
                nToDeleteSize = nTweets - maxSize;
            }
            cursor.close();
            if (nToDeleteSize > 0) {
                // Find SENT_DATE of the most recent tweet to delete
                cursor = mContentResolver.query(mContentUri, new String[] { AndTweetDatabase.Tweets.SENT_DATE },
                        null, null, "sent ASC LIMIT 0," + nToDeleteSize);
                if (cursor.moveToLast()) {
                    sinceTimestampSize = cursor.getLong(0);
                }
                cursor.close();
                if (sinceTimestampSize > 0) {
                    SelectionAndArgs sa = new SelectionAndArgs();
                    sa.addSelection(AndTweetDatabase.Tweets.SENT_DATE + " <=  ?",
                            new String[] { String.valueOf(sinceTimestampSize) });
                    if (mTimelineType != AndTweetDatabase.Tweets.TIMELINE_TYPE_MESSAGES) {
                        sa.addSelection(AndTweetDatabase.Tweets.FAVORITED + " = ?", new String[] { "0" });
                    }
                    nDeletedSize = mContentResolver.delete(mContentUri, sa.selection, sa.selectionArgs);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "pruneOldRecords failed");
            e.printStackTrace();
        }
    }
    nDeleted = nDeletedTime + nDeletedSize;
    if (MyLog.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "pruneOldRecords; History time=" + maxDays + " days; deleted " + nDeletedTime + " , since "
                + sinceTimestamp + ", now=" + System.currentTimeMillis());
        Log.v(TAG, "pruneOldRecords; History size=" + maxSize + " tweets; deleted " + nDeletedSize + " of "
                + nTweets + " tweets, since " + sinceTimestampSize);
    }

    return nDeleted;
}

From source file:gov.wa.wsdot.android.wsdot.service.CamerasSyncService.java

/** 
 * Check the camera table for any starred entries. If we find some, save them
 * to a list so we can re-star those cameras after we flush the database.
 *///from  ww w.  j a v a  2 s. c  o m
private List<Integer> getStarred() {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = null;
    List<Integer> starred = new ArrayList<Integer>();

    try {
        cursor = resolver.query(Cameras.CONTENT_URI, new String[] { Cameras.CAMERA_ID },
                Cameras.CAMERA_IS_STARRED + "=?", new String[] { "1" }, null);

        if (cursor != null && cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                starred.add(cursor.getInt(0));
                cursor.moveToNext();
            }
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return starred;
}

From source file:cn.code.notes.gtask.data.SqlNote.java

private void loadFromCursor(Cursor c) {
    mId = c.getLong(ID_COLUMN);// w  ww. j  a  v a2 s .co  m
    mAlertDate = c.getLong(ALERTED_DATE_COLUMN);
    mBgColorId = c.getInt(BG_COLOR_ID_COLUMN);
    mCreatedDate = c.getLong(CREATED_DATE_COLUMN);
    mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN);
    mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN);
    mParentId = c.getLong(PARENT_ID_COLUMN);
    mSnippet = c.getString(SNIPPET_COLUMN);
    mType = c.getInt(TYPE_COLUMN);
    mWidgetId = c.getInt(WIDGET_ID_COLUMN);
    mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);
    mVersion = c.getLong(VERSION_COLUMN);
}

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.
 *///from ww  w. ja v a2s . c  o  m
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.wildplot.android.ankistats.CollectionData.java

private void loadCol() {
    Cursor cursor = null;
    try {/*from  w  w  w.j a v  a2  s. c o m*/
        // Read in deck table columns
        cursor = mAnkiDb.getDatabase()
                .rawQuery("SELECT crt, mod, scm, dty, usn, ls, " + "conf, decks, dconf FROM col", null);
        if (!cursor.moveToFirst()) {
            return;
        }
        mCrt = cursor.getLong(0);
        mMod = cursor.getLong(1);
        mScm = cursor.getLong(2);
        mDty = cursor.getInt(3) == 1; // No longer used
        mUsn = cursor.getInt(4);
        mLs = cursor.getLong(5);
        try {
            mConf = new JSONObject(cursor.getString(6));
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        loadDecks(cursor.getString(7), cursor.getString(8));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public boolean historyIsVazio() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/*from ww w. j av  a2  s. c om*/
    Cursor c = null;
    db = helper.getWritableDatabase();
    c = db.rawQuery("SELECT count(" + DBHelper.ID_COL + ") FROM " + DBHelper.TABLE_NAME, null);
    if (c != null && c.moveToFirst() && c.getInt(0) > 0) {
        Log.i("Script: ", "Contem dados no histrico!");
        return true;
    }
    Log.i("Script: ", "No contem nunhum dado no histrico!");
    return false;
}

From source file:github.popeen.dsub.util.SongDBHandler.java

public synchronized Pair<Integer, String> getIdFromPath(int serverKey, String path) {
    SQLiteDatabase db = this.getReadableDatabase();

    String[] columns = { SONGS_SERVER_KEY, SONGS_SERVER_ID };
    Cursor cursor = db.query(TABLE_SONGS, columns,
            SONGS_SERVER_KEY + " = ? AND " + SONGS_COMPLETE_PATH + " = ?",
            new String[] { Integer.toString(serverKey), path }, null, null, null, null);

    try {//w ww . java2 s .c o m
        cursor.moveToFirst();
        return new Pair(cursor.getInt(0), cursor.getString(1));
    } catch (Exception e) {
        return null;
    } finally {
        db.close();
    }
}

From source file:de.uni_koblenz_landau.apow.db.SyncDBHelper.java

/**
 * Is row modified?/* w w w . j a  va2  s .com*/
  * @param table Table
 * @param uuid UUID
 * @return -1 if new, 0 if not modified, 1 if modified
 */
private int isDirty(String table, String uuid) {
    int dirty = -1;
    String query = "SELECT dirty FROM " + table + " WHERE uuid = ?;";
    Cursor cursor = mDatabase.rawQuery(query, new String[] { uuid });
    cursor.moveToFirst();
    if (!cursor.isAfterLast()) {
        dirty = cursor.getInt(0);
    }
    cursor.close();
    return dirty;
}