public static String singleValueFromQuery(SQLiteDatabase database, String query) { Cursor cursor = database.rawQuery(query, new String[] {}); String value = ""; if (cursor != null) { cursor.moveToFirst(); value = cursor.getString(0); cursor.close(); } return value; }
@Override protected JSONArray doInBackground(String... params) { JSONArray json = null; SQLiteDatabase db = mDBHelper.getReadableDatabase(); try { json = constructResponse(db.rawQuery(generateQuery(params[0]), new String[] {}), params[0]); } catch (JSONException e) { e.printStackTrace(); } return json; }
@Override public boolean execute(SQLiteDatabase database) { database.execSQL("create table t1(a INTEGER, b INTEGER);"); database.execSQL("insert into t1(a,b) values(123, 456);"); Cursor cursor = database.rawQuery("select * from t1;", new String[] {}); if (cursor != null) { cursor.moveToFirst(); cursor.copyStringToBuffer(1, charArrayBuffer); String actualValue = new String(charArrayBuffer.data, 0, charArrayBuffer.sizeCopied); return "456".equals(actualValue); } return false; }
private void buildSQL() throws SQLException { StringBuilder sb = new StringBuilder(128); sb.append("INSERT INTO "); sb.append(mTableName); sb.append(" ("); StringBuilder sbv = new StringBuilder(128); sbv.append("VALUES ("); int i = 1; Cursor cur = null; try { cur = mDb.rawQuery("PRAGMA table_info(" + mTableName + ")", null); mColumns = new HashMap<String, Integer>(cur.getCount()); while (cur.moveToNext()) { String columnName = cur.getString(TABLE_INFO_PRAGMA_COLUMNNAME_INDEX); String defaultValue = cur.getString(TABLE_INFO_PRAGMA_DEFAULT_INDEX); mColumns.put(columnName, i); sb.append("'"); sb.append(columnName); sb.append("'"); if (defaultValue == null) { sbv.append("?"); } else { sbv.append("COALESCE(?, "); sbv.append(defaultValue); sbv.append(")"); } sb.append(i == cur.getCount() ? ") " : ", "); sbv.append(i == cur.getCount() ? ");" : ", "); ++i; } } finally { if (cur != null) cur.close(); } sb.append(sbv); mInsertSQL = sb.toString(); if (LOCAL_LOGV) Log.v(TAG, "insert statement is " + mInsertSQL); }
public JSONArray getObservationsFor(JSONObject params) throws JSONException { String patientUuid = params.getString("patientUuid"); JSONArray conceptNamesArray = params.getJSONArray("conceptNames"); String conceptNames = conceptNamesArray.toString(); JSONArray visitUuidsArray = params.getJSONArray("visitUuids"); String visitUuids = visitUuidsArray.toString(); SQLiteDatabase db = mDBHelper.getReadableDatabase(); JSONArray observations = new JSONArray(); String inClauseConceptNameList = conceptNamesArray.length() > 0 ? conceptNames.substring(2, conceptNames.length() - 2) : conceptNames; String inClauseVisitUuidsList = visitUuidsArray.length() > 0 ? visitUuids.substring(2, visitUuids.length() - 2) : visitUuids; Cursor c = db.rawQuery( "SELECT observationJson from observation" + " WHERE patientUuid = '" + patientUuid + "'" + " AND conceptName in (\"" + inClauseConceptNameList + "\") " + " AND ( visitUuid in (\"" + inClauseVisitUuidsList + "\") OR visitUuid is NULL )", new String[] {}); if (c.getCount() < 1) { c.close(); return new JSONArray(); } c.moveToFirst(); for (int index = 0; index < c.getCount(); index++) { JSONObject observationJson = new JSONObject(); observationJson.put( "observation", new JSONObject(c.getString(c.getColumnIndex("observationJson")))); observations.put(index, observationJson); c.moveToNext(); } c.close(); return observations; }
public JSONArray getObservationsForVisit(String visitUuid) throws JSONException { SQLiteDatabase db = mDBHelper.getReadableDatabase(); Cursor c = db.rawQuery( "SELECT observationJson FROM observation WHERE visitUuid = '" + visitUuid + "'", new String[] {}); JSONArray observations = new JSONArray(); if (c.getCount() < 1) { c.close(); return observations; } c.moveToFirst(); for (int index = 0; index < c.getCount(); index++) { JSONObject observationJson = new JSONObject(); observationJson.put( "observation", new JSONObject(c.getString(c.getColumnIndex("observationJson")))); observations.put(index, observationJson); c.moveToNext(); } return observations; }