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, String limit) 

Source Link

Document

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

Usage

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

private void processRobots(JSONArray robots) {
    try {/*from  w  ww. j a  va  2 s.c o  m*/
        for (int i = 0; i < robots.length(); i++) {
            JSONObject row = robots.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(ROBOT_LU_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(ROBOT_LU_Entry.COLUMN_NAME_ID, row.getInt(ROBOT_LU_Entry.COLUMN_NAME_ID));
            vals.put(ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID, row.getString(ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID));
            vals.put(ROBOT_LU_Entry.COLUMN_NAME_ROBOT_PHOTO,
                    row.getString(ROBOT_LU_Entry.COLUMN_NAME_ROBOT_PHOTO));
            vals.put(ROBOT_LU_Entry.COLUMN_NAME_TIMESTAMP,
                    DB.dateParser.format(new Date(row.getLong(ROBOT_LU_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { ROBOT_LU_Entry.COLUMN_NAME_ID };
            String[] where = { vals.getAsString(ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID) };
            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(ROBOT_LU_Entry.TABLE_NAME, projection, // select
                        ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(ROBOT_LU_Entry.TABLE_NAME, vals, ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID + " = ?",
                                where);
                        break;
                    case INSERT:
                        db.insert(ROBOT_LU_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(ROBOT_LU_Entry.TABLE_NAME, ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID + " = ?",
                                where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }
        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

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

private void processWheelBase(JSONArray wheelBase) {
    try {/*www. j  a v  a 2  s .  co  m*/
        for (int i = 0; i < wheelBase.length(); i++) {
            JSONObject row = wheelBase.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(WHEEL_BASE_LU_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(WHEEL_BASE_LU_Entry.COLUMN_NAME_ID, row.getInt(WHEEL_BASE_LU_Entry.COLUMN_NAME_ID));
            vals.put(WHEEL_BASE_LU_Entry.COLUMN_NAME_WHEEL_BASE_DESC,
                    row.getString(WHEEL_BASE_LU_Entry.COLUMN_NAME_WHEEL_BASE_DESC));
            vals.put(WHEEL_BASE_LU_Entry.COLUMN_NAME_TIMESTAMP, DB.dateParser
                    .format(new Date(row.getLong(WHEEL_BASE_LU_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { WHEEL_BASE_LU_Entry.COLUMN_NAME_WHEEL_BASE_DESC };
            String[] where = { vals.getAsString(WHEEL_BASE_LU_Entry.COLUMN_NAME_ID) };

            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(WHEEL_BASE_LU_Entry.TABLE_NAME, projection, // select
                        WHEEL_BASE_LU_Entry.COLUMN_NAME_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(WHEEL_BASE_LU_Entry.TABLE_NAME, vals,
                                WHEEL_BASE_LU_Entry.COLUMN_NAME_ID + " = ?", where);
                        break;
                    case INSERT:
                        db.insert(WHEEL_BASE_LU_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(WHEEL_BASE_LU_Entry.TABLE_NAME, WHEEL_BASE_LU_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }
        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

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

private void processWheelType(JSONArray wheelType) {
    try {/*from ww  w.  j  a  va2s. c  om*/
        for (int i = 0; i < wheelType.length(); i++) {
            JSONObject row = wheelType.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(WHEEL_TYPE_LU_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(WHEEL_TYPE_LU_Entry.COLUMN_NAME_ID, row.getInt(WHEEL_TYPE_LU_Entry.COLUMN_NAME_ID));
            vals.put(WHEEL_TYPE_LU_Entry.COLUMN_NAME_WHEEL_TYPE_DESC,
                    row.getString(WHEEL_TYPE_LU_Entry.COLUMN_NAME_WHEEL_TYPE_DESC));
            vals.put(WHEEL_TYPE_LU_Entry.COLUMN_NAME_TIMESTAMP, DB.dateParser
                    .format(new Date(row.getLong(WHEEL_TYPE_LU_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { WHEEL_TYPE_LU_Entry.COLUMN_NAME_WHEEL_TYPE_DESC };
            String[] where = { vals.getAsString(WHEEL_TYPE_LU_Entry.COLUMN_NAME_ID) };

            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(WHEEL_TYPE_LU_Entry.TABLE_NAME, projection, // select
                        WHEEL_TYPE_LU_Entry.COLUMN_NAME_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(WHEEL_TYPE_LU_Entry.TABLE_NAME, vals,
                                WHEEL_TYPE_LU_Entry.COLUMN_NAME_ID + " = ?", where);
                        break;
                    case INSERT:
                        db.insert(WHEEL_TYPE_LU_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(WHEEL_TYPE_LU_Entry.TABLE_NAME, WHEEL_TYPE_LU_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }
        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

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

private void processPositions(JSONArray positions) {
    try {/*w  ww.  ja  va 2s .com*/
        for (int i = 0; i < positions.length(); i++) {
            JSONObject row = positions.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(POSITION_LU_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(POSITION_LU_Entry.COLUMN_NAME_ID, row.getInt(POSITION_LU_Entry.COLUMN_NAME_ID));
            vals.put(POSITION_LU_Entry.COLUMN_NAME_POSITION,
                    row.getString(POSITION_LU_Entry.COLUMN_NAME_POSITION));
            vals.put(POSITION_LU_Entry.COLUMN_NAME_TIMESTAMP, DB.dateParser
                    .format(new Date(row.getLong(POSITION_LU_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { POSITION_LU_Entry.COLUMN_NAME_POSITION };
            String[] where = { vals.getAsString(POSITION_LU_Entry.COLUMN_NAME_ID) };

            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(POSITION_LU_Entry.TABLE_NAME, projection, // select
                        POSITION_LU_Entry.COLUMN_NAME_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(POSITION_LU_Entry.TABLE_NAME, vals, POSITION_LU_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    case INSERT:
                        db.insert(POSITION_LU_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(POSITION_LU_Entry.TABLE_NAME, POSITION_LU_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }
        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

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

private void processEvents(JSONArray events) {
    try {//from ww  w . j  a  v a 2s.c o m
        for (int i = 0; i < events.length(); i++) {
            JSONObject row = events.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(EVENT_LU_Entry.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = new ContentValues();
            vals.put(EVENT_LU_Entry.COLUMN_NAME_ID, row.getInt(EVENT_LU_Entry.COLUMN_NAME_ID));
            vals.put(EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME,
                    row.getString(EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME));
            vals.put(EVENT_LU_Entry.COLUMN_NAME_EVENT_CODE,
                    row.getString(EVENT_LU_Entry.COLUMN_NAME_EVENT_CODE));
            vals.put(EVENT_LU_Entry.COLUMN_NAME_DATE_START,
                    row.getString(EVENT_LU_Entry.COLUMN_NAME_DATE_START));
            vals.put(EVENT_LU_Entry.COLUMN_NAME_TIMESTAMP,
                    DB.dateParser.format(new Date(row.getLong(EVENT_LU_Entry.COLUMN_NAME_TIMESTAMP) * 1000)));

            // check if this entry exists already
            String[] projection = { EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME };
            String[] where = { vals.getAsString(EVENT_LU_Entry.COLUMN_NAME_ID) };
            synchronized (ScoutingDBHelper.lock) {

                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(EVENT_LU_Entry.TABLE_NAME, projection, // select
                        EVENT_LU_Entry.COLUMN_NAME_ID + "=?", where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    }

                    switch (action) {
                    case UPDATE:
                        db.update(EVENT_LU_Entry.TABLE_NAME, vals, EVENT_LU_Entry.COLUMN_NAME_ID + " = ?",
                                where);
                        break;
                    case INSERT:
                        db.insert(EVENT_LU_Entry.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(EVENT_LU_Entry.TABLE_NAME, EVENT_LU_Entry.COLUMN_NAME_ID + " = ?", where);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }

        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

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

private void processMatches(JSONArray matches) {
    // TODO could be abstracted further
    try {/*from ww  w . ja  va  2  s .c o m*/
        for (int i = 0; i < matches.length(); i++) {
            JSONObject row = matches.getJSONObject(i);
            Action action = Action.UPDATE;
            if (row.getInt(MatchStatsStruct.COLUMN_NAME_INVALID) != 0) {
                action = Action.DELETE;
            }
            ContentValues vals = MatchStatsStruct.getNewMatchStats().jsonToCV(row);

            // check if this entry exists already
            String[] projection = { MatchStatsStruct.COLUMN_NAME_ID, MatchStatsStruct.COLUMN_NAME_INVALID };
            String[] where = { vals.getAsString(MatchStatsStruct.COLUMN_NAME_EVENT_ID),
                    vals.getAsString(MatchStatsStruct.COLUMN_NAME_MATCH_ID),
                    vals.getAsString(MatchStatsStruct.COLUMN_NAME_TEAM_ID),
                    vals.getAsString(MatchStatsStruct.COLUMN_NAME_PRACTICE_MATCH) };

            synchronized (ScoutingDBHelper.lock) {
                SQLiteDatabase db = ScoutingDBHelper.getInstance().getWritableDatabase();

                Cursor c = db.query(MatchStatsStruct.TABLE_NAME, projection, // select
                        MatchStatsStruct.COLUMN_NAME_EVENT_ID + "=? AND "
                                + MatchStatsStruct.COLUMN_NAME_MATCH_ID + "=? AND "
                                + MatchStatsStruct.COLUMN_NAME_TEAM_ID + "=? AND "
                                + MatchStatsStruct.COLUMN_NAME_PRACTICE_MATCH + "=?",
                        where, null, // don't
                        // group
                        null, // don't filter
                        null, // don't order
                        "0,1"); // limit to 1
                try {
                    int id = 0, invalid = 0;
                    if (!c.moveToFirst()) {
                        if (action == Action.UPDATE)
                            action = Action.INSERT;
                        else if (action == Action.DELETE)
                            action = Action.NOTHING;
                    } else {
                        id = c.getInt(c.getColumnIndexOrThrow(MatchStatsStruct.COLUMN_NAME_ID));
                        invalid = c.getInt(c.getColumnIndexOrThrow(MatchStatsStruct.COLUMN_NAME_INVALID));
                        if (invalid > 0) // this field has not been sent to
                                         // server yet.
                            action = Action.NOTHING;
                    }

                    String[] where2 = { String.valueOf(id) };

                    switch (action) {
                    case UPDATE:
                        db.update(MatchStatsStruct.TABLE_NAME, vals, MatchStatsStruct.COLUMN_NAME_ID + " = ?",
                                where2);
                        break;
                    case INSERT:
                        db.insert(MatchStatsStruct.TABLE_NAME, null, vals);
                        break;
                    case DELETE:
                        db.delete(MatchStatsStruct.TABLE_NAME, MatchStatsStruct.COLUMN_NAME_ID + " = ?",
                                where2);
                        break;
                    default:
                    }
                } finally {
                    if (c != null)
                        c.close();
                    ScoutingDBHelper.getInstance().close();
                }
            }
        }
    } catch (JSONException e) {
        // TODO handle error
    }
}

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

public String getCodeFromEventName(String eventName) {
    synchronized (ScoutingDBHelper.lock) {
        try {//from   w  w w .  j  a  v a2  s .co  m
            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();
            String[] projection = { EVENT_LU_Entry.COLUMN_NAME_EVENT_CODE };
            String[] where = { eventName };
            Cursor c = db.query(EVENT_LU_Entry.TABLE_NAME, // from the
                    // event_lu
                    // table
                    projection, // select
                    EVENT_LU_Entry.COLUMN_NAME_EVENT_NAME + " LIKE ?", // where
                    // event_name
                    // ==
                    where, // EventName
                    null, // don't group
                    null, // don't filter
                    null, // don't order
                    "0,1"); // limit to 1
            String ret = "";
            try {
                c.moveToFirst();
                ret = c.getString(c.getColumnIndexOrThrow(EVENT_LU_Entry.COLUMN_NAME_EVENT_CODE));
            } 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 PitStats getTeamPitStats(int teamNum) {

    synchronized (ScoutingDBHelper.lock) {

        try {//  www  .  j a v a  2s  . c om
            PitStats stats = PitStats.getNewPitStats();

            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();

            String[] projection = stats.getProjection();
            String[] where = { String.valueOf(teamNum) };
            Cursor c = db.query(PitStats.TABLE_NAME, // from the
                    // scout_pit_data
                    // table
                    projection, // select
                    PitStats.COLUMN_NAME_TEAM_ID + "=?", // where team_id ==
                    where, // teamNum
                    null, // don't group
                    null, // don't filter
                    null, // don't order
                    "0,1"); // limit to 1
            try {

                stats.fromCursor(c, this, db);

            } finally {
                if (c != null)
                    c.close();
                ScoutingDBHelper.getInstance().close();
            }

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

    }
}

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

public String getPictureURL(int teamNum) {
    synchronized (ScoutingDBHelper.lock) {
        String ret = "";
        try {//  w w  w.j  a va2  s  .c  o m

            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();

            String[] projection = { ROBOT_LU_Entry.COLUMN_NAME_ROBOT_PHOTO };
            String[] where = { String.valueOf(teamNum) };
            Cursor c = db.query(ROBOT_LU_Entry.TABLE_NAME, // from the
                    // robot_lu
                    // table
                    projection, // select
                    ROBOT_LU_Entry.COLUMN_NAME_TEAM_ID + "=?", // where
                    // team_id
                    // ==
                    where, // teamNum
                    null, // don't group
                    null, // don't filter
                    null, // don't order
                    "0,1"); // limit to 1
            try {

                if (c.moveToFirst()) {
                    ret = c.getString(c.getColumnIndexOrThrow(ROBOT_LU_Entry.COLUMN_NAME_ROBOT_PHOTO));
                }

            } 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 String getTeamPitInfo(String teamNum) {

    synchronized (ScoutingDBHelper.lock) {
        try {/*w w  w.j a  va  2 s.  c o m*/

            SQLiteDatabase db = ScoutingDBHelper.getInstance().getReadableDatabase();
            String date = "";

            String[] projection = { PitStats.COLUMN_NAME_TIMESTAMP };
            String[] where = { teamNum };
            Cursor c = db.query(PitStats.TABLE_NAME, // from the
                    // scout_pit_data
                    // table
                    projection, // select
                    PitStats.COLUMN_NAME_TEAM_ID + "=?", // where
                    // team_id
                    // ==
                    where, // teamNum
                    null, // don't group
                    null, // don't filter
                    null, // don't order
                    "0,1"); // limit to 1

            try {
                c.moveToFirst();

                date = c.getString(c.getColumnIndexOrThrow(PitStats.COLUMN_NAME_TIMESTAMP));
            } finally {
                if (c != null)
                    c.close();
                ScoutingDBHelper.getInstance().close();
            }
            return date;

        } catch (Exception e) {
            return "";
        }
    }
}