List of usage examples for android.database Cursor getCount
int getCount();
From source file:com.example.shutapp.DatabaseHandler.java
/** * @return the amount of chatrooms in the database. *//*from w ww.ja va 2 s.co m*/ public int getChatroomsCount() { String countQuery = "SELECT * FROM " + TABLE_CHATROOMS; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); if (cursor != null) { cursor.moveToFirst(); } int count = cursor.getCount(); cursor.close(); // return count return count; }
From source file:net.naonedbus.manager.impl.FavoriManager.java
/** * Transformer un curseur en liste d'lments * /*from w ww. j av a2 s. co m*/ * @param c * un Curseur * @return une liste d'lments */ protected List<Favori> getFromCursorFull(final Cursor c) { final List<Favori> items = new ArrayList<Favori>(); if (c.getCount() > 0) { c.moveToFirst(); while (!c.isAfterLast()) { items.add(getSingleFromCursorFull(c)); c.moveToNext(); } } c.close(); return items; }
From source file:org.opensmc.mytracks.cyclesmc.TripUploader.java
@Override protected Boolean doInBackground(Long... tripid) { // First, send the trip user asked for: Boolean result = uploadOneTrip(tripid[0]); // TODO: not always working? //Log.d("uploading trip", tripid[0].toString()); ////////////////////////////////////////////// // Then, automatically try and send previously-completed trips // that were not sent successfully. Vector<Long> unsentTrips = new Vector<Long>(); mDb.openReadOnly();//from w w w . j a va 2 s.c o m Cursor cur = mDb.fetchUnsentTrips(); if (cur != null && cur.getCount() > 0) { //pd.setMessage("Sent. You have previously unsent trips; submitting those now."); //////////// //Log.d("previously unsent count", cur.getCount() + " previously unsent trips"); //////////// while (!cur.isAfterLast()) { unsentTrips.add(Long.valueOf(cur.getLong(0))); cur.moveToNext(); } cur.close(); } mDb.close(); for (Long trip : unsentTrips) { result &= uploadOneTrip(trip); /////////////// //Log.d("uploading unsent trip", trip.toString()); /////////////// } return result; }
From source file:com.github.gw2app.events.Gw2ApiEvents.java
private List<Gw2Event> _getEvents(String url) { Log.d("Gw2", "Fetching event names from DB."); SQLiteDatabase db = this.dbhelper.getWritableDatabase(); Cursor cursor = db.query(Gw2DB.EVENT_NAMES_TABLE, null, null, null, null, null, null); HashMap<String, String> eventNames = new HashMap<String, String>(); if (cursor.getCount() == 0) { cursor.close();/*from w w w .j av a 2s. c o m*/ return new ArrayList<Gw2Event>(); } cursor.moveToFirst(); while (!cursor.isLast()) { String id = cursor.getString(0); String name = cursor.getString(1); eventNames.put(id, name); cursor.moveToNext(); } cursor.close(); Log.d("Gw2", "Fetching event data from JSON"); ArrayList<Gw2Event> list = new ArrayList<Gw2Event>(); try { //Log.d("Gw2", url); String result = this.fetchJSONfromURL(url); JSONObject jsData = new JSONObject(result); JSONArray jsArray = jsData.getJSONArray("events"); for (int i = 0; i < jsArray.length(); i++) { JSONObject obj = jsArray.getJSONObject(i); int world_id = obj.getInt("world_id"); int map_id = obj.getInt("map_id"); String event_id = obj.getString("event_id"); String state = obj.getString("state"); list.add(new Gw2Event(world_id, map_id, event_id, state, eventNames.get(event_id))); } } catch (JSONException e) { e.printStackTrace(); } return list; }
From source file:com.seneca.android.senfitbeta.DbHelper.java
public boolean getUser(String email, String pass) { String selectQuery = "select * from " + USER_TABLE + " where " + COLUMN_EMAIL + " = " + "'" + email + "'" + " and " + COLUMN_PASS + " = " + "'" + pass + "'"; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); cursor.moveToFirst();//from w w w . java2s.co m if (cursor.getCount() > 0) { return true; } cursor.close(); db.close(); return false; }
From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java
private SQLitePLuginResult doSelectInBackgroundAndPossiblyThrow(String sql, String[] bindArgs, SQLiteDatabase db) {// ww w.j a va2s . c om debug("\"all\" query: %s", sql); Cursor cursor = null; try { cursor = db.rawQuery(sql, bindArgs); int numRows = cursor.getCount(); if (numRows == 0) { return EMPTY_RESULT; } int numColumns = cursor.getColumnCount(); Object[][] rows = new Object[numRows][]; String[] columnNames = cursor.getColumnNames(); for (int i = 0; cursor.moveToNext(); i++) { Object[] row = new Object[numColumns]; for (int j = 0; j < numColumns; j++) { row[j] = getValueFromCursor(cursor, j, cursor.getType(j)); } rows[i] = row; } debug("returning %d rows", numRows); return new SQLitePLuginResult(rows, columnNames, 0, 0, null); } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.yuntongxun.ecdemo.storage.IMessageSqlManager.java
/** * @param/*from ww w.j a v a2 s .c o m*/ */ public static void deleteLocalFileAfterFire(String msgId) { if (TextUtils.isEmpty(msgId)) { return; } String text = ""; 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()) { text = cursor.getString(cursor.getColumnIndex("localPath")); cursor.close(); cursor = null; } } if (!TextUtils.isEmpty(text)) { File file = new File(text); if (file != null && file.exists()) { file.delete(); } } }
From source file:com.example.igorklimov.popularmoviesdemo.sync.SyncAdapter.java
private void getData() { int sortByPreference = Utility.getSortByPreference(mContext); Uri contentUri = null;// www .ja v a 2 s. c o m if (sortByPreference != 4) { String sortType = null; switch (sortByPreference) { case 1: sortType = POPULARITY_DESC; contentUri = MovieByPopularity.CONTENT_URI; break; case 2: sortType = formatDate(); contentUri = MovieByReleaseDate.CONTENT_URI; break; case 3: sortType = VOTE_AVG_DESC; contentUri = MovieByVotes.CONTENT_URI; break; } Cursor cursor = mContentResolver.query(contentUri, null, null, null, null); int page = Utility.getPagePreference(mContext); int count = cursor.getCount(); if (count == 0) page = 1; Log.d(LOG_TAG, "onPerformSync: cursorCount " + count + " page: " + page); if (count < page * 20) { String jsonResponse = getJsonResponse(DISCOVER_MOVIES + SORT_BY + sortType + PAGE + page + API_KEY); if (jsonResponse != null) { JSONObject[] JsonMovies = getJsonMovies(jsonResponse); try { Log.d(LOG_TAG, "INSERTING EXTRA DATA"); int i = 0; for (JSONObject jsonMovie : JsonMovies) { String poster = IMAGE_BASE + W_185 + jsonMovie.getString("poster_path"); String backdropPath = IMAGE_BASE + "/w500" + jsonMovie.getString("backdrop_path"); String title = jsonMovie.getString("title"); String releaseDate = jsonMovie.getString("release_date"); String vote = jsonMovie.getString("vote_average"); String plot = jsonMovie.getString("overview"); String genres = Utility.formatGenres(getGenres(jsonMovie)); String id = jsonMovie.getString("id"); ContentValues values = new ContentValues(); values.put(COLUMN_TITLE, title); values.put(COLUMN_POSTER, poster); values.put(COLUMN_RELEASE_DATE, releaseDate); values.put(COLUMN_GENRES, genres); values.put(COLUMN_MOVIE_ID, id); values.put(COLUMN_BACKDROP_PATH, backdropPath); values.put(COLUMN_AVERAGE_VOTE, vote); values.put(COLUMN_PLOT, plot); mContentValues[i++] = values; } int bulkInsert = mContentResolver.bulkInsert(contentUri, mContentValues); Log.d(LOG_TAG, "onPerformSync: bulkInsert " + bulkInsert); cursor.close(); } catch (JSONException e) { e.printStackTrace(); } } } } }
From source file:fr.free.nrw.commons.contributions.ContributionDao.java
public Contribution fromCursor(Cursor cursor) { // Hardcoding column positions! //Check that cursor has a value to avoid CursorIndexOutOfBoundsException if (cursor.getCount() > 0) { int index; if (cursor.getColumnIndex(Table.COLUMN_LICENSE) == -1) { index = 15;/* ww w . j av a2 s. c o m*/ } else { index = cursor.getColumnIndex(Table.COLUMN_LICENSE); } Contribution contribution = new Contribution( uriForId(cursor.getInt(cursor.getColumnIndex(Table.COLUMN_ID))), cursor.getString(cursor.getColumnIndex(Table.COLUMN_FILENAME)), parseUri(cursor.getString(cursor.getColumnIndex(Table.COLUMN_LOCAL_URI))), cursor.getString(cursor.getColumnIndex(Table.COLUMN_IMAGE_URL)), parseTimestamp(cursor.getLong(cursor.getColumnIndex(Table.COLUMN_TIMESTAMP))), cursor.getInt(cursor.getColumnIndex(Table.COLUMN_STATE)), cursor.getLong(cursor.getColumnIndex(Table.COLUMN_LENGTH)), parseTimestamp(cursor.getLong(cursor.getColumnIndex(Table.COLUMN_UPLOADED))), cursor.getLong(cursor.getColumnIndex(Table.COLUMN_TRANSFERRED)), cursor.getString(cursor.getColumnIndex(Table.COLUMN_SOURCE)), cursor.getString(cursor.getColumnIndex(Table.COLUMN_DESCRIPTION)), cursor.getString(cursor.getColumnIndex(Table.COLUMN_CREATOR)), cursor.getInt(cursor.getColumnIndex(Table.COLUMN_MULTIPLE)) == 1, cursor.getInt(cursor.getColumnIndex(Table.COLUMN_WIDTH)), cursor.getInt(cursor.getColumnIndex(Table.COLUMN_HEIGHT)), cursor.getString(index)); String wikidataEntityId = cursor.getString(cursor.getColumnIndex(COLUMN_WIKI_DATA_ENTITY_ID)); if (!StringUtils.isBlank(wikidataEntityId)) { contribution.setWikiDataEntityId(wikidataEntityId); } return contribution; } return null; }
From source file:ch.ethz.twimight.net.tds.TDSRequestMessage.java
public void createDisTweetsObject(Cursor tweets) throws JSONException { if (tweets != null && tweets.getCount() > 0) { tweets.moveToFirst();//w w w. ja v a2 s . co m disTweetsObject = new JSONObject(); JSONArray disTweetsArray = new JSONArray(); CertificateManager cm = new CertificateManager(context.getApplicationContext()); while (!tweets.isAfterLast()) { if (!tweets.isNull(tweets.getColumnIndex(Tweets.COL_SIGNATURE))) { JSONObject row = new JSONObject(); row.put(Tweets.COL_TEXT_PLAIN, tweets.getString(tweets.getColumnIndex(Tweets.COL_TEXT_PLAIN))); row.put(Tweets.COL_USER_TID, tweets.getLong(tweets.getColumnIndex(Tweets.COL_USER_TID))); row.put(Tweets.COL_DISASTER_ID, tweets.getLong(tweets.getColumnIndex(Tweets.COL_DISASTER_ID))); row.put(Tweets.COL_SIGNATURE, tweets.getString(tweets.getColumnIndex(Tweets.COL_SIGNATURE))); SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); row.put(Tweets.COL_CREATED_AT + "_phone", simpleFormat .format(new Date(tweets.getLong(tweets.getColumnIndex(Tweets.COL_CREATED_AT))))); // add picture if available if (tweets.getString(tweets.getColumnIndex(Tweets.COL_MEDIA_URIS)) != null) { SDCardHelper sdCardHelper = new SDCardHelper(); String base64Photo = sdCardHelper.getImageAsBas64Jpeg( tweets.getString(tweets.getColumnIndex(Tweets.COL_MEDIA_URIS)), null); Log.d(TAG, base64Photo); row.put(PHOTO, base64Photo); } disTweetsArray.put(row); } tweets.moveToNext(); } tweets.close(); if (disTweetsArray.length() > 0) disTweetsObject.put("content", disTweetsArray); else disTweetsObject = null; } }