Cursor cursor = db.rawQuery(...); try { while (cursor.moveToNext()) { ... } } finally { cursor.close(); }
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while(people.moveToNext()) { int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); String contact = people.getString(nameFieldColumnIndex); int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER); String number = people.getString(numberFieldColumnIndex); } people.close();
public boolean isRead(String table, String key, int value) { boolean isRead = false; Cursor cursor = mSQLiteDatabase.query(table, null, "key=?", new String[]{key}, null, null, null); if (cursor.moveToNext() && (cursor.getInt(cursor.getColumnIndex("is_read")) == value)) { isRead = true; } cursor.close(); return isRead; }
/** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */ public List<RelationSource2> loadAllDeepFromCursor(Cursor cursor) { int count = cursor.getCount(); List<RelationSource2> list = new ArrayList<RelationSource2>(count); if (cursor.moveToFirst()) { if (identityScope != null) { identityScope.lock(); identityScope.reserveRoom(count); } try { do { list.add(loadCurrentDeep(cursor, false)); } while (cursor.moveToNext()); } finally { if (identityScope != null) { identityScope.unlock(); } } } return list; }
// public static final String INBOX = "content://sms/inbox"; // public static final String SENT = "content://sms/sent"; // public static final String DRAFT = "content://sms/draft"; Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null); if (cursor.moveToFirst()) { // must check the result to prevent exception do { String msgData = ""; for(int idx=0;idx<cursor.getColumnCount();idx++) { msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx); } // use msgData } while (cursor.moveToNext()); } else { // empty box, no SMS }
@Override public String apply(Query query) { Cursor cursor = query.run(); try { if (!cursor.moveToNext()) { throw new AssertionError("No rows"); } return cursor.getString(0); } finally { cursor.close(); } } });
@Test public void testExecSQL() throws Exception { database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');"); Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM table_name", null); assertThat(cursor).isNotNull(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(1); cursor = database.rawQuery("SELECT * FROM table_name", null); assertThat(cursor).isNotNull(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(cursor.getColumnIndex("id"))).isEqualTo(1234); assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Chuck"); }
public void testQuery() { SimpleEntity entity = new SimpleEntity(); entity.setSimpleString("hello"); daoSession.insert(entity); long id = entity.getId(); SimpleEntity entity2 = new SimpleEntity(); entity2.setSimpleString("content"); daoSession.insert(entity2); long id2 = entity2.getId(); Cursor cursor = getContext().getContentResolver().query(SimpleEntityContentProvider.CONTENT_URI, null, null, null, "_id"); assertEquals(2, cursor.getCount()); int idxId = cursor.getColumnIndexOrThrow(SimpleEntityDao.Properties.Id.columnName); int idxString = cursor.getColumnIndexOrThrow(SimpleEntityDao.Properties.SimpleString.columnName); assertTrue(cursor.moveToFirst()); assertEquals("hello", cursor.getString(idxString)); assertEquals(id, cursor.getLong(idxId)); assertTrue(cursor.moveToNext()); assertEquals("content", cursor.getString(idxString)); assertEquals(id2, cursor.getLong(idxId)); }
Cursor cursor; ... //fill the cursor here for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { // do what you need with the cursor here }
@Test public void testIsNullWhenNull() throws Exception { assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.isNull(cursor.getColumnIndex("id"))).isFalse(); assertThat(cursor.isNull(cursor.getColumnIndex("name"))).isFalse(); assertThat(cursor.isNull(cursor.getColumnIndex("long_value"))).isTrue(); assertThat(cursor.isNull(cursor.getColumnIndex("float_value"))).isTrue(); assertThat(cursor.isNull(cursor.getColumnIndex("double_value"))).isTrue(); }
/** Returns the count (number of results matching the query). Uses SELECT COUNT (*) sematics. */ public long count() { checkThread(); Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); try { if (!cursor.moveToNext()) { throw new DaoException("No result for count"); } else if (!cursor.isLast()) { throw new DaoException("Unexpected row count: " + cursor.getCount()); } else if (cursor.getColumnCount() != 1) { throw new DaoException("Unexpected column count: " + cursor.getColumnCount()); } return cursor.getLong(0); } finally { cursor.close(); } }
ContentResolver contentResolver = getContentResolver(); final String[] projection = new String[]{"_id", "ct_t"}; Uri uri = Uri.parse("content://mms-sms/conversations/"); Cursor query = contentResolver.query(uri, projection, null, null, null); if (query.moveToFirst()) { do { String string = query.getString(query.getColumnIndex("ct_t")); if ("application/vnd.wap.multipart.related".equals(string)) { // it's MMS } else { // it's SMS } } while (query.moveToNext()); }
@Test public void testGetString() throws Exception { assertThat(cursor.moveToFirst()).isTrue(); String[] data = {"Chuck", "Julie", "Chris"}; for (String aData : data) { assertThat(cursor.getString(1)).isEqualTo(aData); cursor.moveToNext(); } }
public static String queryString(Database db, String sql) { Cursor cursor = db.rawQuery(sql, null); try { assertTrue(cursor.moveToNext()); return cursor.getString(0); } finally { cursor.close(); } }
@Test public void query_shouldReturnColumnValues() throws Exception { ShadowDownloadManager manager = new ShadowDownloadManager(); long id = manager.enqueue(request.setDestinationUri(destination)); Cursor cursor = manager.query(new DownloadManager.Query().setFilterById(id)); cursor.moveToNext(); assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))).isEqualTo(uri.toString()); assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).isEqualTo(destination.toString()); }
@Test public void testMoveToNextPastEnd() throws Exception { assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.moveToNext()).isFalse(); }
if (Integer.parseInt(cur.getString( cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); while (pCur.moveToNext()) { // Do something with phones } pCur.close(); }
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close();
@Test public void testMoveToNext() throws Exception { assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(1235); assertThat(cursor.getString(1)).isEqualTo("Julie"); }
public void isExhausted() { if (cursor.moveToNext()) { StringBuilder data = new StringBuilder(); for (int i = 0; i < cursor.getColumnCount(); i++) { if (i > 0) data.append(", "); data.append(cursor.getString(i)); } throw new AssertionError("Expected no more rows but was: " + data); } cursor.close(); } }