List of usage examples for android.database Cursor getLong
long getLong(int columnIndex);
From source file:com.hichinaschool.flashcards.libanki.Finder.java
public static List<Pair<String, List<Long>>> findDupes(Collection col, String fieldName, String search) { // limit search to notes with applicable field name if (search != null && search.length() > 0) { search = "(" + search + ") "; }//from www .ja va 2s .com search += "'" + fieldName + ":*'"; // go through notes String sql = "select id, mid, flds from notes where id in " + Utils.ids2str(col.findNotes(search).toArray(new Long[] {})); Cursor cur = null; Map<Long, Integer> fields = new HashMap<Long, Integer>(); Map<String, List<Long>> vals = new HashMap<String, List<Long>>(); List<Pair<String, List<Long>>> dupes = new ArrayList<Pair<String, List<Long>>>(); try { cur = col.getDb().getDatabase().rawQuery(sql, null); while (cur.moveToNext()) { long nid = cur.getLong(0); long mid = cur.getLong(1); String[] flds = Utils.splitFields(cur.getString(2)); // inlined ordForMid(mid) if (!fields.containsKey(mid)) { JSONObject model = col.getModels().get(mid); fields.put(mid, col.getModels().fieldMap(model).get(fieldName).first); } String val = flds[fields.get(mid)]; // empty does not count as duplicate if (val.equals("")) { continue; } if (!vals.containsKey(val)) { vals.put(val, new ArrayList<Long>()); } vals.get(val).add(nid); if (vals.get(val).size() == 2) { dupes.add(new Pair<String, List<Long>>(val, vals.get(val))); } } } finally { if (cur != null) { cur.close(); } } return dupes; }
From source file:android.database.DatabaseUtils.java
/** * Reads a Long out of a column in a Cursor and writes it to a ContentValues. * Adds nothing to the ContentValues if the column isn't present or if its value is null. * * @param cursor The cursor to read from * @param column The column to read/* w w w .j a va 2 s. co m*/ * @param values The {@link ContentValues} to put the value into */ public static void cursorLongToContentValuesIfPresent(Cursor cursor, ContentValues values, String column) { final int index = cursor.getColumnIndexOrThrow(column); if (!cursor.isNull(index)) { values.put(column, cursor.getLong(index)); } }
From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java
public static Vector<Long> getPendingOutgoingMessageIds(Context context) { Vector<Long> retVal = null; DBHelper db = new DBHelper(context); SQLiteDatabase dbread = db.getReadableDatabase(); Cursor c = dbread.rawQuery("SELECT _id FROM offline_sent_posts", null); int count = c.getCount(); if (count == 0) { retVal = new Vector<Long>(0); } else {// ww w . j av a 2 s . c om retVal = new Vector<Long>(count); c.moveToFirst(); for (int i = 0; i < count; i++) { retVal.add(c.getLong(0)); c.moveToNext(); } } c.close(); dbread.close(); db.close(); return retVal; }
From source file:android.database.DatabaseUtils.java
/** * Reads a Long out of a field in a Cursor and writes it to a Map. * * @param cursor The cursor to read from * @param field The INTEGER field to read * @param values The {@link ContentValues} to put the value into * @param key The key to store the value with in the map *///from w ww .j av a 2 s. co m public static void cursorLongToContentValues(Cursor cursor, String field, ContentValues values, String key) { int colIndex = cursor.getColumnIndex(field); if (!cursor.isNull(colIndex)) { Long value = Long.valueOf(cursor.getLong(colIndex)); values.put(key, value); } else { values.put(key, (Long) null); } }
From source file:com.hichinaschool.flashcards.libanki.Finder.java
public static int findReplace(Collection col, List<Long> nids, String src, String dst, boolean isRegex, String field, boolean fold) { Map<Long, Integer> mmap = new HashMap<Long, Integer>(); if (field != null) { try {/*from w w w .j a v a 2 s. co m*/ for (JSONObject m : col.getModels().all()) { JSONArray flds = m.getJSONArray("flds"); for (int fi = 0; fi < flds.length(); ++fi) { JSONObject f = flds.getJSONObject(fi); if (f.getString("name").equals(field)) { mmap.put(m.getLong("id"), f.getInt("ord")); } } } } catch (JSONException e) { throw new RuntimeException(e); } if (mmap.isEmpty()) { return 0; } } // find and gather replacements if (!isRegex) { src = Pattern.quote(src); } if (fold) { src = "(?i)" + src; } Pattern regex = Pattern.compile(src); ArrayList<Object[]> d = new ArrayList<Object[]>(); String sql = "select id, mid, flds from notes where id in " + Utils.ids2str(nids.toArray(new Long[] {})); nids = new ArrayList<Long>(); Cursor cur = null; try { cur = col.getDb().getDatabase().rawQuery(sql, null); while (cur.moveToNext()) { String flds = cur.getString(2); String origFlds = flds; // does it match? String[] sflds = Utils.splitFields(flds); if (field != null) { long mid = cur.getLong(1); if (!mmap.containsKey(mid)) { continue; } int ord = mmap.get(mid); sflds[ord] = regex.matcher(sflds[ord]).replaceAll(dst); } else { for (int i = 0; i < sflds.length; ++i) { sflds[i] = regex.matcher(sflds[i]).replaceAll(dst); } } flds = Utils.joinFields(sflds); if (!flds.equals(origFlds)) { long nid = cur.getLong(0); nids.add(nid); d.add(new Object[] { flds, Utils.intNow(), col.usn(), nid }); } } } finally { if (cur != null) { cur.close(); } } if (d.isEmpty()) { return 0; } // replace col.getDb().executeMany("update notes set flds=?,mod=?,usn=? where id=?", d); long[] pnids = Utils.toPrimitive(nids); col.updateFieldCache(pnids); col.genCards(pnids); return d.size(); }
From source file:com.ichi2.libanki.Finder.java
/** * @return List of Pair("dupestr", List[nids]) *///from w w w. j ava 2 s .c o m public static List<Pair<String, List<Long>>> findDupes(Collection col, String fieldName, String search) { // limit search to notes with applicable field name if (!TextUtils.isEmpty(search)) { search = "(" + search + ") "; } search += "'" + fieldName + ":*'"; // go through notes Map<String, List<Long>> vals = new HashMap<String, List<Long>>(); List<Pair<String, List<Long>>> dupes = new ArrayList<Pair<String, List<Long>>>(); Map<Long, Integer> fields = new HashMap<Long, Integer>(); Cursor cur = null; try { cur = col.getDb().getDatabase().rawQuery( "select id, mid, flds from notes where id in " + Utils.ids2str(col.findNotes(search)), null); while (cur.moveToNext()) { long nid = cur.getLong(0); long mid = cur.getLong(1); String[] flds = Utils.splitFields(cur.getString(2)); Integer ord = ordForMid(col, fields, mid, fieldName); if (ord == null) { continue; } String val = flds[fields.get(mid)]; val = Utils.stripHTMLMedia(val); // empty does not count as duplicate if (TextUtils.isEmpty(val)) { continue; } if (!vals.containsKey(val)) { vals.put(val, new ArrayList<Long>()); } vals.get(val).add(nid); if (vals.get(val).size() == 2) { dupes.add(new Pair<String, List<Long>>(val, vals.get(val))); } } } finally { if (cur != null) { cur.close(); } } return dupes; }
From source file:com.liferay.alerts.database.DatabaseHelper.java
private void _convertMessageToJSONObject(SQLiteDatabase database) { StringBuilder select = new StringBuilder(); select.append("SELECT "); select.append(Alert.ID);/*from w w w . j ava2s . co m*/ select.append(", "); select.append(Alert.PAYLOAD); select.append(" FROM "); select.append(AlertDAO.TABLE_NAME); Cursor cursor = database.rawQuery(select.toString(), null); while (cursor.moveToNext()) { try { long id = cursor.getLong(cursor.getColumnIndex(Alert.ID)); String payload = cursor.getString(cursor.getColumnIndex(Alert.PAYLOAD)); JSONObject jsonObject = new JSONObject(); jsonObject.put(Alert.MESSAGE, payload); ContentValues values = new ContentValues(); values.put(Alert.PAYLOAD, jsonObject.toString()); StringBuilder sb = new StringBuilder(); sb.append(Alert.ID); sb.append(CharPool.SPACE); sb.append("= ?"); String whereClause = sb.toString(); String[] whereArgs = { String.valueOf(id) }; database.update(AlertDAO.TABLE_NAME, values, whereClause, whereArgs); } catch (JSONException je) { Log.e(_TAG, "Couldn't convert message.", je); } } }
From source file:org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase.java
@Override protected List<Cookie> loadCookies() { final List<Cookie> list = new LinkedList<Cookie>(); final Cursor cursor = myDatabase .rawQuery("SELECT cookie_id,host,path,name,value,date_of_expiration,secure FROM Cookie", null); while (cursor.moveToNext()) { final long id = cursor.getLong(0); final String host = cursor.getString(1); final String path = cursor.getString(2); final String name = cursor.getString(3); final String value = cursor.getString(4); final Date date = SQLiteUtil.getDate(cursor, 5); final boolean secure = cursor.getLong(6) == 1; Set<Integer> portSet = null; final Cursor portsCursor = myDatabase.rawQuery("SELECT port FROM CookiePort WHERE cookie_id = " + id, null);/*from w w w . ja v a2s . c o m*/ while (portsCursor.moveToNext()) { if (portSet == null) { portSet = new HashSet<Integer>(); } portSet.add((int) portsCursor.getLong(1)); } portsCursor.close(); final BasicClientCookie2 c = new BasicClientCookie2(name, value); c.setDomain(host); c.setPath(path); if (portSet != null) { final int ports[] = new int[portSet.size()]; int index = 0; for (int p : portSet) { ports[index] = p; ++index; } c.setPorts(ports); } c.setExpiryDate(date); c.setSecure(secure); c.setDiscard(false); list.add(c); } cursor.close(); return list; }
From source file:net.bible.service.db.mynote.MyNoteDBAdapter.java
/** return Dto from current cursor position or null * @param c// w ww . j av a 2 s. c om * @return * @throws NoSuchKeyException */ private MyNoteDto getMyNoteDto(Cursor c) { MyNoteDto dto = new MyNoteDto(); try { //Id Long id = c.getLong(MyNoteQuery.ID); dto.setId(id); //Verse String key = c.getString(MyNoteQuery.KEY); Versification v11n = null; if (!c.isNull(MyNoteQuery.VERSIFICATION)) { String v11nString = c.getString(MyNoteQuery.VERSIFICATION); if (!StringUtils.isEmpty(v11nString)) { v11n = Versifications.instance().getVersification(v11nString); } } if (v11n == null) { Log.d(TAG, "Using default Versification"); // use default v11n v11n = Versifications.instance().getVersification(Versifications.DEFAULT_V11N); } Log.d(TAG, "Versification found:" + v11n); try { dto.setVerse(VerseFactory.fromString(v11n, key)); } catch (Exception e) { Log.e(TAG, "Note saved with incorrect versification", e); // fix problem where KJV was always the v11n even for dc books // NRSVA should contain most dc books and allow verse to be fetched Versification v11nWithDC = Versifications.instance().getVersification("NRSVA"); dto.setVerse(VerseFactory.fromString(v11nWithDC, key)); } //Note String mynote = c.getString(MyNoteQuery.MYNOTE); dto.setNoteText(mynote); //Update date long updated = c.getLong(MyNoteQuery.LAST_UPDATED_ON); dto.setLastUpdatedOn(new Date(updated)); //Create date long created = c.getLong(MyNoteQuery.CREATED_ON); dto.setCreatedOn(new Date(created)); } catch (NoSuchKeyException nke) { Log.e(TAG, "Key error", nke); } return dto; }
From source file:com.bellman.bible.service.db.mynote.MyNoteDBAdapter.java
/** * return Dto from current cursor position or null * * @param c/*from w w w . j a v a 2 s . c o m*/ * @return * @throws NoSuchKeyException */ private MyNoteDto getMyNoteDto(Cursor c) { MyNoteDto dto = new MyNoteDto(); try { //Id Long id = c.getLong(MyNoteQuery.ID); dto.setId(id); //Verse String key = c.getString(MyNoteQuery.KEY); Versification v11n = null; if (!c.isNull(MyNoteQuery.VERSIFICATION)) { String v11nString = c.getString(MyNoteQuery.VERSIFICATION); if (!StringUtils.isEmpty(v11nString)) { v11n = Versifications.instance().getVersification(v11nString); } } if (v11n == null) { Log.d(TAG, "Using default Versification"); // use default v11n v11n = Versifications.instance().getVersification(Versifications.DEFAULT_V11N); } Log.d(TAG, "Versification found:" + v11n); try { dto.setVerseRange(VerseRangeFactory.fromString(v11n, key)); } catch (Exception e) { Log.e(TAG, "Note saved with incorrect versification", e); // fix problem where KJV was always the v11n even for dc books // NRSVA should contain most dc books and allow verse to be fetched Versification v11nWithDC = Versifications.instance().getVersification("NRSVA"); dto.setVerseRange(VerseRangeFactory.fromString(v11nWithDC, key)); } //Note String mynote = c.getString(MyNoteQuery.MYNOTE); dto.setNoteText(mynote); //Update date long updated = c.getLong(MyNoteQuery.LAST_UPDATED_ON); dto.setLastUpdatedOn(new Date(updated)); //Create date long created = c.getLong(MyNoteQuery.CREATED_ON); dto.setCreatedOn(new Date(created)); } catch (NoSuchKeyException nke) { Log.e(TAG, "Key error", nke); } return dto; }