Code example for Cursor

Methods: closegetBlobgetColumnIndexisAfterLastmoveToFirst

0
        Cursor cursor = builder.query(db, columns, selection, selectionArgs,
                groupBy, having, sortOrder);
 
        if (cursor == null) {
            return null; 
        } else if (!cursor.moveToFirst()) {
            cursor.close();
            return null; 
        } 
        return cursor;
    } 
 
    public SortedMap<Long, Sample> queryOldestSamples(int howmany) {
        SortedMap<Long, Sample> results = new TreeMap<Long, Sample>();
        try { 
            synchronized (dbLock) {
                if (db == null || !db.isOpen()) {
                	try{ 
                		db = helper.getWritableDatabase();
                	}catch (android.database.sqlite.SQLiteException ex){
                		Log.e(TAG, "Could not open database", ex);
                		return results;
                	} 
                } 
                String[] columns = mColumnMap.keySet().toArray(
                        new String[mColumnMap.size()]);
 
                Cursor cursor = query(null, null, columns, null, null,
                        COLUMN_TIMESTAMP + " ASC LIMIT " + howmany);
 
                if (cursor == null) {
                    // There are no results 
                    return results;
                } else { 
                    cursor.moveToFirst();
                    while (!cursor.isAfterLast()) {
                        Sample s = fillSample(cursor);
                        results.put(cursor.getLong(cursor
                                .getColumnIndex(BaseColumns._ID)), s);
                        cursor.moveToNext();
                    } 
                    cursor.close();
 
                } 
            } 
        } catch (Throwable th) {
            Log.e(TAG, "Failed to query oldest samples!", th);
        } 
        return results;
    } 
 
    private int delete(String whereClause, String[] whereArgs) {
        int deleted = db.delete(SAMPLES_VIRTUAL_TABLE, whereClause, whereArgs);
        return deleted;
    } 
 
    public int deleteSamples(Set<Long> rowids) {
        int ret = 0;
        try { 
            synchronized (dbLock) {
                if (db == null || !db.isOpen()) {
                    db = helper.getWritableDatabase();
                } 
                StringBuilder sb = new StringBuilder();
                int i = 0;
                sb.append("(");
                for (Long rowid : rowids) {
                    sb.append("" + rowid);
                    i++;
                    if (i != rowids.size()) {
                        sb.append(", ");
                    } 
                } 
                sb.append(")");
                Log.d("CaratSampleDB",
                        "Deleting where rowid in " + sb.toString());
                ret = delete("rowid in " + sb.toString(), null);
 
                if (db != null && db.isOpen()) {
                    db.close();
                } 
            } 
        } catch (Throwable th) {
            Log.e(TAG, "Failed to delete samples!", th);
        } 
        return ret;
    } 
 
    private Sample queryLastSample() { 
        String[] columns = mColumnMap.keySet().toArray(
                new String[mColumnMap.size()]);
 
        Cursor cursor = query(null, null, columns, null, null, COLUMN_TIMESTAMP
                + " DESC LIMIT 1"); 
 
        if (cursor == null) {
            // There are no results 
            return null; 
        } else { 
            cursor.moveToFirst();
            if (!cursor.isAfterLast()) {
                Sample s = fillSample(cursor);
                cursor.close();
                lastSample = s;
                return s;
            } 
            cursor.close();
            return null; 
        } 
    } 
 
    /* 
     * Read a sample from the current position of the cursor. TODO: Needs to be 
     * updated when fields update. 
     */ 
    private Sample fillSample(Cursor cursor) {
        Sample s = null;
        byte[] sampleB = cursor.getBlob(cursor
                .getColumnIndex(CaratSampleDB.COLUMN_SAMPLE));
        if (sampleB != null) {
            ObjectInputStream oi;
            try { 
                oi = new ObjectInputStream(new ByteArrayInputStream(sampleB));
                Object o = oi.readObject();