Cursor countCursor = getContentResolver().query(CONTENT_URI, new String[] {"count(*) AS count"}, null, null, null); countCursor.moveToFirst(); int count = countCursor.getInt(0);
String selection = "_id = "+id; Uri uri = Uri.parse("content://sms"); Cursor cursor = contentResolver.query(uri, null, selection, null, null); String phone = cursor.getString(cursor.getColumnIndex("address")); int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc. String date = cursor.getString(cursor.getColumnIndex("date")); String body = cursor.getString(cursor.getColumnIndex("body"));
Cursor mCount= db.rawQuery("select count(*) from users where uname='" + loginname + "' and pwd='" + loginpass +"'", null); mCount.moveToFirst(); int count= mCount.getInt(0); mCount.close();
Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM mytable " + "where Age > 10 LIMIT 5", null); if (c != null ) { if (c.moveToFirst()) { do { String firstName = c.getString(c.getColumnIndex("FirstName")); int age = c.getInt(c.getColumnIndex("Age")); results.add("" + firstName + ",Age: " + age); }while (c.moveToNext()); } } c.close();
@Override public TestEntity readEntity(Cursor cursor, int offset) { TestEntity entity = new TestEntity( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id cursor.getInt(offset + 1), // simpleInt cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2), // simpleInteger cursor.getString(offset + 3), // simpleStringNotNull cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // simpleString cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // indexedString cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // indexedStringAscUnique cursor.isNull(offset + 7) ? null : new java.util.Date(cursor.getLong(offset + 7)), // simpleDate cursor.isNull(offset + 8) ? null : cursor.getShort(offset + 8) != 0, // simpleBoolean cursor.isNull(offset + 9) ? null : cursor.getBlob(offset + 9) // simpleByteArray ); return entity; }
private void writeCursorPartsToOutputStream(SQLiteDatabase db, Cursor cursor, OutputStream outputStream) throws IOException, MessagingException { while (cursor.moveToNext()) { String partId = cursor.getString(ATTACH_PART_ID_INDEX); int location = cursor.getInt(ATTACH_LOCATION_INDEX); if (location == DataLocation.IN_DATABASE || location == DataLocation.ON_DISK) { writeSimplePartToOutputStream(partId, cursor, outputStream); } else if (location == DataLocation.CHILD_PART_CONTAINS_DATA) { writeRawBodyToStream(cursor, db, outputStream); } } }
@Override public Integer apply(Query query) { Cursor cursor = query.run(); try { if (!cursor.moveToNext()) { throw new AssertionError("No rows"); } return cursor.getInt(0); } finally { cursor.close(); } } });
private Uri getVideoUri(int position) { videos.moveToPosition(position); return(ContentUris.withAppendedId( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videos.getInt( videos.getColumnIndex(MediaStore.Video.Media._ID)))); } }
@Test public void testMoveToFirst() throws Exception { assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(1234); assertThat(cursor.getString(1)).isEqualTo("Chuck"); }
/** * Check if download was valid, see issue * http://code.google.com/p/android/issues/detail?id=18462 * @param long1 * @return */ private boolean validDownload(long downloadId) { Log.d(TAG,"Checking download status for id: " + downloadId); //Verify if download is a success Cursor c= dMgr.query(new DownloadManager.Query().setFilterById(downloadId)); if(c.moveToFirst()){ int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); if(status == DownloadManager.STATUS_SUCCESSFUL){ return true; //Download is valid, celebrate }else{ int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON)); Log.d(TAG, "Download not correct, status [" + status + "] reason [" + reason + "]"); return false; } } return false; }
public void testCursorQuerySimple() { insert(3); CursorQuery query = dao.queryBuilder().orderAsc(Properties.SimpleInteger).buildCursor(); Cursor cursor = query.query(); try { assertEquals(3, cursor.getCount()); assertTrue(cursor.moveToNext()); int columnIndex = cursor.getColumnIndexOrThrow(Properties.SimpleInteger.columnName); assertEquals(getSimpleInteger(0), cursor.getInt(columnIndex)); assertTrue(cursor.moveToNext()); assertEquals(getSimpleInteger(1), cursor.getInt(columnIndex)); assertTrue(cursor.moveToNext()); assertEquals(getSimpleInteger(2), cursor.getInt(columnIndex)); assertFalse(cursor.moveToNext()); } finally { cursor.close(); } }
@Test public void testGetInt() throws Exception { assertThat(cursor.moveToFirst()).isTrue(); int[] data = {1234, 1235, 1236}; for (int aData : data) { assertThat(cursor.getInt(0)).isEqualTo(aData); cursor.moveToNext(); } }
@Test public void testFailureNestedTransaction() throws Exception { database.beginTransaction(); database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');"); database.beginTransaction(); database.execSQL("INSERT INTO table_name (id, name) VALUES(12345, 'Julie');"); database.endTransaction(); database.setTransactionSuccessful(); database.endTransaction(); Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM table_name", null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(0); }
public static List<Album> getAlbumsForCursor(Cursor cursor) { ArrayList arrayList = new ArrayList(); if ((cursor != null) && (cursor.moveToFirst())) do { arrayList.add(new Album(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getLong(3), cursor.getInt(4), cursor.getInt(5))); } while (cursor.moveToNext()); if (cursor != null) cursor.close(); return arrayList; }
void bindModel(Cursor row) { int mimeTypeColumn= row.getColumnIndex(MediaStore.Video.Media.MIME_TYPE); videoUri=ContentUris.withAppendedId( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, row.getInt(row.getColumnIndex(MediaStore.Video.Media._ID))); videoMimeType=row.getString(mimeTypeColumn); } }
@Override protected int getExifOrientation(Uri uri) { Cursor cursor = null; try { ContentResolver contentResolver = context.getContentResolver(); cursor = contentResolver.query(uri, CONTENT_ORIENTATION, null, null, null); if (cursor == null || !cursor.moveToFirst()) { return 0; } return cursor.getInt(0); } catch (RuntimeException ignored) { // If the orientation column doesn't exist, assume no rotation. return 0; } finally { if (cursor != null) { cursor.close(); } } }
@Override public SpecialNamesEntity readEntity(Cursor cursor, int offset) { SpecialNamesEntity entity = new SpecialNamesEntity( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // count cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // select cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // sum cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // avg cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // join cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // distinct cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // on cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // index cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9) // order ); return entity; }
@Test public void testFailureTransaction() throws Exception { database.beginTransaction(); database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');"); final String select = "SELECT COUNT(*) FROM table_name"; Cursor cursor = database.rawQuery(select, null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(1); cursor.close(); database.endTransaction(); cursor = database.rawQuery(select, null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(0); }
private Uri getVideoUri(int position) { videos.moveToPosition(position); return(ContentUris.withAppendedId( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videos.getInt(videos.getColumnIndex(MediaStore.Video.Media._ID)))); } }
Uri imageUri = intent.getData(); String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null); int orientation = -1; if (cur != null && cur.moveToFirst()) { orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); } Matrix matrix = new Matrix(); matrix.postRotate(orientation);