Example usage for android.database.sqlite SQLiteDatabase query

List of usage examples for android.database.sqlite SQLiteDatabase query

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase query.

Prototype

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy,
        String having, String orderBy) 

Source Link

Document

Query the given table, returning a Cursor over the result set.

Usage

From source file:org.frc836.database.DB.java

public List<String> getEventList() {

    synchronized (ScoutingDBHelper.lock) {
        try {// w  w w.jav  a  2s.  co  m
            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();

            String[] projection = { EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME };

            Cursor c = db.query(EVENT_LU_Entry.TABLE_NAME, projection, null, null, null, null,
                    EVENT_LU_Entry.COLUMN_NAME_ID);
            List<String> ret;
            try {

                ret = new ArrayList<String>(c.getCount());

                if (c.moveToFirst())
                    do {
                        ret.add(c.getString(c.getColumnIndexOrThrow(EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME)));
                    } while (c.moveToNext());
                else
                    ret = null;
            } finally {
                if (c != null)
                    c.close();
                ScoutingDBHelper.getInstance().close();
            }

            return ret;
        } catch (Exception e) {
            return null;
        }
    }
}

From source file:org.frc836.database.DB.java

public List<String> getNotesOptions() {
    synchronized (ScoutingDBHelper.lock) {
        try {//from   w w w . j  a  v  a  2  s  .  c o m
            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();

            String[] projection = { NOTES_OPTIONS_Entry.COLUMN_NAME_OPTION_TEXT };

            Cursor c = db.query(NOTES_OPTIONS_Entry.TABLE_NAME, projection, null, null, null, null,
                    NOTES_OPTIONS_Entry.COLUMN_NAME_ID);
            List<String> ret;
            try {

                ret = new ArrayList<String>(c.getCount());

                if (c.moveToFirst())
                    do {
                        ret.add(c.getString(
                                c.getColumnIndexOrThrow(NOTES_OPTIONS_Entry.COLUMN_NAME_OPTION_TEXT)));
                    } while (c.moveToNext());
                else
                    ret = null;
            } finally {
                if (c != null)
                    c.close();
                ScoutingDBHelper.getInstance().close();
            }

            return ret;
        } catch (Exception e) {
            return null;
        }
    }
}

From source file:org.frc836.database.DB.java

public List<String> getWheelTypeList() {

    synchronized (ScoutingDBHelper.lock) {
        try {/* ww  w .j av a2 s .c  o  m*/
            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();

            String[] projection = { WHEEL_TYPE_LU_Entry.COLUMN_NAME_WHEEL_TYPE_DESC };

            Cursor c = db.query(WHEEL_TYPE_LU_Entry.TABLE_NAME, projection, null, null, null, null,
                    WHEEL_TYPE_LU_Entry.COLUMN_NAME_ID);
            List<String> ret;
            try {

                ret = new ArrayList<String>(c.getCount());

                if (c.moveToFirst())
                    do {
                        ret.add(c.getString(
                                c.getColumnIndexOrThrow(WHEEL_TYPE_LU_Entry.COLUMN_NAME_WHEEL_TYPE_DESC)));
                    } while (c.moveToNext());
                else
                    ret = null;
            } finally {
                if (c != null)
                    c.close();
                ScoutingDBHelper.getInstance().close();
            }

            return ret;
        } catch (Exception e) {
            return null;
        }
    }
}

From source file:org.frc836.database.DB.java

public List<String> getWheelBaseList() {

    synchronized (ScoutingDBHelper.lock) {
        try {/* w ww. j a va2  s  .co m*/
            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();

            String[] projection = { WHEEL_BASE_LU_Entry.COLUMN_NAME_WHEEL_BASE_DESC };

            Cursor c = db.query(WHEEL_BASE_LU_Entry.TABLE_NAME, projection, null, null, null, null,
                    WHEEL_BASE_LU_Entry.COLUMN_NAME_ID);
            List<String> ret;

            try {

                ret = new ArrayList<String>(c.getCount());

                if (c.moveToFirst())
                    do {
                        ret.add(c.getString(
                                c.getColumnIndexOrThrow(WHEEL_BASE_LU_Entry.COLUMN_NAME_WHEEL_BASE_DESC)));
                    } while (c.moveToNext());
                else
                    ret = null;
            } finally {
                if (c != null)
                    c.close();
                ScoutingDBHelper.getInstance().close();
            }

            return ret;
        } catch (Exception e) {
            return null;
        }
    }
}

From source file:org.frc836.database.DB.java

public List<String> getConfigList() {

    synchronized (ScoutingDBHelper.lock) {
        try {//www  .  ja  va2s  . c o  m
            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();

            String[] projection = { CONFIGURATION_LU_Entry.COLUMN_NAME_CONFIGURATION_DESC };

            Cursor c = db.query(CONFIGURATION_LU_Entry.TABLE_NAME, projection, null, null, null, null,
                    CONFIGURATION_LU_Entry.COLUMN_NAME_ID);
            List<String> ret;
            try {

                ret = new ArrayList<String>(c.getCount());

                if (c.moveToFirst())
                    do {
                        ret.add(c.getString(c
                                .getColumnIndexOrThrow(CONFIGURATION_LU_Entry.COLUMN_NAME_CONFIGURATION_DESC)));
                    } while (c.moveToNext());
                else
                    ret = null;
            } finally {
                if (c != null)
                    c.close();
                ScoutingDBHelper.getInstance().close();
            }

            return ret;
        } catch (Exception e) {
            return null;
        }
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

private Cursor queryBuilder(String selection, String[] selectionArgs, String sortBy) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String outerJoin = createOuterJoin(RECIPES_TABLE, TAGS_TABLE, RT_ID, TT_RECIPE_ID);
        String[] fields = RECIPES_FIELDS;
        fields[0] = RECIPES_TABLE + "." + RT_ID;
        return db.query(outerJoin, fields, selection, selectionArgs, fields[0], null, sortBy);
    }/* w  ww .j a  va 2 s  .co m*/
}

From source file:com.raspi.chatapp.util.storage.MessageHistory.java

public MessageArrayContent getMessage(String buddyId, long messageId) {
    int index = buddyId.indexOf('@');
    if (index >= 0) {
        buddyId = buddyId.substring(0, index);
    }//from ww  w.j a va  2 s .  c  o m
    MessageArrayContent mac = null;
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    String[] columns = new String[] { MessageHistoryContract.MessageEntry.COLUMN_NAME_BUDDY_ID,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_TYPE,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_CONTENT,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_URL,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_STATUS,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_TIMESTAMP,
            MessageHistoryContract.MessageEntry._ID,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_OTHERS_ID };
    String sel = MessageHistoryContract.MessageEntry._ID + "=?";
    Cursor message = db.query(buddyId, columns, sel, new String[] { String.valueOf(messageId) }, null, null,
            null);

    message.moveToFirst();
    String from = message.getString(0);
    SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCES, 0);
    String me = preferences.getString(Constants.USERNAME, "");
    String type = message.getString(1);
    String content = message.getString(2);
    String url = message.getString(3);
    int progress = 0;
    String status = message.getString(4);
    long time = message.getLong(5);
    long _ID = message.getLong(6);
    long othersId = message.getLong(7);
    switch (type) {
    case (MessageHistory.TYPE_TEXT):
        mac = new TextMessage(!me.equals(from), content, time, status, _ID, othersId);
        break;
    case (MessageHistory.TYPE_IMAGE):
        try {
            JSONArray contentJSON = new JSONArray(content);
            mac = new ImageMessage(!me.equals(from), //left
                    contentJSON.getString(0), //File
                    contentJSON.getString(1), //description
                    url, //url
                    progress, //progress
                    time, //timeStamp
                    status, //status
                    _ID, //_ID
                    buddyId, //buddyID
                    othersId); //othersId
        } catch (Exception e) {
            e.printStackTrace();
        }
        break;
    }
    db.close();
    return mac;
}

From source file:org.ohmage.db.DbHelper.java

public boolean populatePromptsFromResponseJSON(SQLiteDatabase db, long responseRowID, String response,
        String campaignUrn, String surveyId) {
    try {/*from   ww w .  ja v a2  s .c o  m*/
        // create a list of metadata for this survey from the surveyprompts table
        // this will help in remapping values for single and multichoice prompts, etc.
        HashMap<String, SurveyPrompt> promptsMap = new HashMap<String, SurveyPrompt>();
        List<SurveyPrompt> promptsList = SurveyPrompt.fromCursor(db.query(Tables.SURVEY_PROMPTS, null,
                SurveyPrompts.COMPOSITE_ID + "='" + campaignUrn + ":" + surveyId + "'", null, null, null,
                null));
        // remap list to hashmap; i know this looks crazy, but it'll make lookups slightly faster and doesn't take much memory
        for (SurveyPrompt sp : promptsList)
            promptsMap.put(sp.mPromptID, sp);

        // convert response data to JSON for parsing
        JSONArray responseData = new JSONArray(response);

        // iterate through the responses and add them to the prompt table one by one
        for (int i = 0; i < responseData.length(); ++i) {
            // nab the jsonobject, which contains "prompt_id" and "value"
            JSONObject item = responseData.getJSONObject(i);

            // if the entry we're looking at doesn't include prompt_id or value, continue
            if (!item.has("prompt_id") || !item.has("value"))
                continue;

            // look up the metadata for this prompt
            SurveyPrompt promptData = promptsMap.get(item.getString("prompt_id"));

            // construct a new PromptResponse object to populate
            PromptResponse p = new PromptResponse();

            p.mCompositeID = campaignUrn + ":" + surveyId;
            p.mResponseID = responseRowID;
            p.mPromptID = item.getString("prompt_id");

            // determine too if we have to remap the value from a number to text
            // if custom_choices is included, then we do
            try {
                // before we determine what to do for this prompt, we need to see if we
                // have metadata. if we don't, we have to go for the default behavior.
                if (promptData == null)
                    throw new NoMetadataException();

                if (item.has("custom_choices")) {
                    // build a hashmap of ID->label so we can do the remapping
                    JSONArray choicesArray = item.getJSONArray("custom_choices");
                    HashMap<String, String> glossary = new HashMap<String, String>();

                    for (int iv = 0; iv < choicesArray.length(); ++iv) {
                        JSONObject choiceObject = choicesArray.getJSONObject(iv);
                        glossary.put(choiceObject.getString("choice_id"),
                                choiceObject.getString("choice_value"));
                    }

                    // determine if the value is singular or an array
                    // if it's an array, we need to remap each element
                    try {
                        JSONArray remapper = item.getJSONArray("value");

                        for (int ir = 0; ir < remapper.length(); ++ir)
                            remapper.put(ir, glossary.get(remapper.getString(ir)));

                        p.mValue = remapper.toString();
                    } catch (JSONException e) {
                        // it wasn't a json array, so just remap the single value
                        p.mValue = glossary.get(item.getString("value"));
                    }
                } else if (promptData.mPromptType.equalsIgnoreCase("single_choice")) {
                    // unload the json properties
                    JSONArray values = new JSONArray(promptData.mProperties);
                    // set the explicit value as the default; if we don't find a match, it'll end up as this
                    p.mValue = item.getString("value");

                    // search for a key that matches the given value
                    for (int ir = 0; ir < values.length(); ++ir) {
                        JSONObject entry = values.getJSONObject(ir);
                        if (entry.getString("key").equals(p.mValue)) {
                            p.mValue = entry.getString("label");
                            p.mExtraValue = item.getString("value");
                            break;
                        }
                    }
                } else if (promptData.mPromptType.equalsIgnoreCase("multi_choice")) {
                    // same procedure as above, except that we need to remap every value

                    try {
                        // unload the json properties
                        JSONArray values = new JSONArray(promptData.mProperties);
                        // set the explicit value as the default; if we don't find a match, it'll end up as this
                        JSONArray newValues = new JSONArray(item.getString("value"));

                        // for each entry in newValues...
                        for (int io = 0; io < newValues.length(); ++io) {
                            // search for a key that matches the given value
                            for (int ir = 0; ir < values.length(); ++ir) {
                                JSONObject entry = values.getJSONObject(ir);
                                if (entry.getString("key").equals(newValues.getString(io))) {
                                    // assign the remapped value to this index
                                    newValues.put(io, entry.getString("label"));
                                    break;
                                }
                            }
                        }

                        // and reassign mValue here
                        p.mValue = newValues.toString();
                        p.mExtraValue = item.getString("value");
                    } catch (JSONException e) {
                        // it wasn't a json array, so just remap the value
                        p.mValue = item.getString("value");
                    }
                } else {
                    p.mValue = item.getString("value");
                }
            } catch (NoMetadataException e) {
                Log.e(TAG, "Couldn't find the associated metadata for prompt ID " + (i + 1)
                        + ", assigning default value");
                p.mValue = item.getString("value");
            }

            // and insert this into prompts            
            db.insert(Tables.PROMPT_RESPONSES, null, p.toCV());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:edu.cens.loci.provider.LociDbUtils.java

public int countPlacesByEntry(int entry) {
    final SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor cursor = db.query(Tables.PLACES, new String[] { Places._ID, Places.ENTRY },
            Places.ENTRY + "=" + entry, null, null, null, null);
    if (cursor != null)
        return cursor.getCount();

    return 0;/*from   w ww.  jav  a 2  s .c  o  m*/
}

From source file:edu.cens.loci.provider.LociDbUtils.java

public int updateData(ContentValues values, String selection, String[] selectionArgs,
        boolean callerIsSyncAdapter) {
    mValues.clear();//w  w w  .j  a  va 2s.co  m
    mValues.putAll(values);
    mValues.remove(Data._ID);
    mValues.remove(Data.PLACE_ID);
    mValues.remove(Data.MIMETYPE);

    int count = 0;
    final SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor c = db.query(Tables.DATA, DataUpdateQuery.COLUMNS, selection, selectionArgs, null, null, null);
    try {
        while (c.moveToNext()) {
            count += updateData(mValues, c, callerIsSyncAdapter);
        }
    } finally {
        c.close();
    }

    return count;
}