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:com.aero2.android.DefaultActivities.SmogMapActivity.java

public boolean addHeatMap(GoogleMap map) {
    Log.v(LOG_TAG, "Entered the addHeatMapFunction");
    AirAzureDbHelper airAzureDbHelper = new AirAzureDbHelper(getApplicationContext());
    final SQLiteDatabase db = airAzureDbHelper.getReadableDatabase();
    //Using the simple cursor loader class to query the cache on a background thread
    SimpleCursorLoader simpleCursorLoader = new SimpleCursorLoader(getApplicationContext()) {
        @Override//from  w  w  w .j  a  v  a  2 s  .com
        public Cursor loadInBackground() {

            //All this work is done in the background thread

            String[] columns = new String[] { AirAzureContract.AirAzureEntry.COLUMN_AIR_INDEX,
                    AirAzureContract.AirAzureEntry.COLUMN_LAT, AirAzureContract.AirAzureEntry.COLUMN_LONG };
            Cursor mCursor;
            mCursor = db.query(AirAzureContract.AirAzureEntry.TABLE_NAME, columns, null, null, null, null,
                    null);
            return mCursor;

        }
    };

    //Getting a cursor containing the map data from the results cache
    Cursor cursor;
    cursor = simpleCursorLoader.loadInBackground();

    //******************************** Setting up the overlay on the Map Using the cursor *************************************
    //   STARTS HERE

    // Create the gradient.
    int[] colors = { Color.rgb(0, 255, 0), // green
            Color.rgb(255, 255, 0), // yellow
            Color.rgb(220, 0, 0) // red
    };

    float[] startPoints = { 0f, 0.5f, 1f };

    Gradient gradient = new Gradient(colors, startPoints);

    Log.v("CusorLength:", " cursor length is " + cursor.getCount());
    if (cursor.getCount() > 0) {
        //Make a Weighted heatmap of the Smog
        HeatmapTileProvider mProvider = new HeatmapTileProvider.Builder()
                .weightedData(getListForHeatMap(cursor)).gradient(gradient).opacity(0.3).radius(10).build();
        map.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
        db.close();
        cursor.close();
        return true;
    } else {
        db.close();
        cursor.close();
        return false;
    }
}

From source file:com.robotoworks.mechanoid.db.SQuery.java

public Cursor query(SQLiteDatabase db, String table, String[] projection, String orderBy) {
    return db.query(table, projection, mBuilder.toString(), getArgsArray(), null, null, orderBy);
}

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

public void checkPlaceTable() {

    if (!LociConfig.D.DB.DEBUG)
        return;//from w  w w.  ja v a2 s . c  om

    final SQLiteDatabase db = mDbHelper.getReadableDatabase();

    Cursor cursor = db.query(Tables.PLACES, null, null, null, null, null, null);

    MyLog.d(LociConfig.D.DB.DEBUG, TAG, "[DB] checkPlaceTable: #places=" + cursor.getCount());

    if (cursor.moveToFirst()) {
        do {
            long id = cursor.getLong(cursor.getColumnIndex(Places._ID));
            String name = cursor.getString(cursor.getColumnIndex(Places.PLACE_NAME));
            int type = cursor.getInt(cursor.getColumnIndex(Places.PLACE_TYPE));
            int state = cursor.getInt(cursor.getColumnIndex(Places.PLACE_STATE));
            MyLog.d(LociConfig.D.DB.DEBUG, TAG,
                    String.format("   id=%d name=%s type=%d state=%d", id, name, type, state));
        } while (cursor.moveToNext());
    }
    cursor.close();
}

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

public void checkDataTable() {

    if (!LociConfig.D.DB.DEBUG)
        return;// w  w  w  . java 2s . c  o m

    final SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor cursor = db.query(Tables.DATA, null, null, null, null, null, null);

    MyLog.d(LociConfig.D.DB.DEBUG, TAG, "[DB] checkDataTable: #rows=" + cursor.getCount());

    if (cursor.moveToFirst()) {
        do {
            long id = cursor.getLong(cursor.getColumnIndex(Data._ID));
            long pid = cursor.getLong(cursor.getColumnIndex(Data.PLACE_ID));
            int mimeTypeId = cursor.getInt(cursor.getColumnIndex(DataColumns.MIMETYPE_ID));

            MyLog.d(LociConfig.D.DB.DEBUG, TAG,
                    String.format("  id=%d pid=%d mimetype=%d", id, pid, mimeTypeId));

        } while (cursor.moveToNext());
    }

    cursor.close();
}

From source file:org.noorganization.instalistsynch.controller.local.dba.impl.SqliteGroupAccessDbControllerTest.java

License:asdf

public void testUpdate() throws Exception {
    Date currentDate = new Date();
    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    ContentValues cv = new ContentValues();
    cv.put(GroupAccess.COLUMN.GROUP_ID, 1);
    cv.put(GroupAccess.COLUMN.INTERRUPTED, false);
    cv.put(GroupAccess.COLUMN.SYNCHRONIZE, true);
    cv.put(GroupAccess.COLUMN.LAST_UPDATE_FROM_SERVER, ISO8601Utils.format(currentDate));
    cv.put(GroupAccess.COLUMN.LAST_TOKEN_REQUEST, ISO8601Utils.format(currentDate));
    cv.put(GroupAccess.COLUMN.TOKEN, "fdskhbvvkddscddueFSNDFSAdnandk3229df-dFSJDKMds.");

    assertTrue(db.insert(GroupAccess.TABLE_NAME, null, cv) >= 0);

    GroupAccess groupAccess = new GroupAccess(1, "fdskhbvvkddscddueasdfeSAdnandk3229df-dFSJDKMds.");
    groupAccess.setLastTokenRequest(currentDate);
    groupAccess.setLastUpdateFromServer(currentDate);
    groupAccess.setSynchronize(true);/*from w  ww.  ja  v a  2s. c  o m*/
    groupAccess.setInterrupted(true);

    assertTrue(mGroupAuthAccessDbController.update(groupAccess));

    Cursor cursor = db.query(GroupAccess.TABLE_NAME, GroupAccess.COLUMN.ALL_COLUMNS,
            GroupAccess.COLUMN.GROUP_ID + " = ? ", new String[] { String.valueOf(1) }, null, null, null);
    int count = cursor.getCount();
    if (count == 0)
        cursor.close();

    assertTrue(cursor.moveToFirst());
    int groupId = cursor.getInt(cursor.getColumnIndex(GroupAccess.COLUMN.GROUP_ID));
    boolean synchronize = cursor.getInt(cursor.getColumnIndex(GroupAccess.COLUMN.SYNCHRONIZE)) == 1;
    boolean interrupted = cursor.getInt(cursor.getColumnIndex(GroupAccess.COLUMN.INTERRUPTED)) == 1;
    Date lastTokenRequestDate = ISO8601Utils.parse(
            cursor.getString(cursor.getColumnIndex(GroupAccess.COLUMN.LAST_TOKEN_REQUEST)),
            new ParsePosition(0));
    Date lastUpdateDate = ISO8601Utils.parse(
            cursor.getString(cursor.getColumnIndex(GroupAccess.COLUMN.LAST_UPDATE_FROM_SERVER)),
            new ParsePosition(0));
    String token = cursor.getString(cursor.getColumnIndex(GroupAccess.COLUMN.TOKEN));
    cursor.close();

    assertEquals(1, groupId);
    assertEquals(true, synchronize);
    assertEquals(true, interrupted);
    assertEquals(ISO8601Utils.format(currentDate), ISO8601Utils.format(lastTokenRequestDate));
    assertEquals(ISO8601Utils.format(currentDate), ISO8601Utils.format(lastUpdateDate));

    assertEquals("fdskhbvvkddscddueasdfeSAdnandk3229df-dFSJDKMds.", token);

}

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

public LociPlace getPlace(long placeId) {
    final SQLiteDatabase db = mDbHelper.getReadableDatabase();

    Cursor cursor = db.query(Tables.PLACES, null, Places._ID + "=" + placeId, null, null, null, null);

    if (cursor.moveToFirst()) {
        LociPlace place = new LociPlace();
        place.placeId = cursor.getLong(cursor.getColumnIndex(Places._ID));
        place.name = cursor.getString(cursor.getColumnIndex(Places.PLACE_NAME));
        place.state = cursor.getInt(cursor.getColumnIndex(Places.PLACE_STATE));
        place.type = cursor.getInt(cursor.getColumnIndex(Places.PLACE_TYPE));
        place.entry = cursor.getInt(cursor.getColumnIndex(Places.ENTRY));
        place.entryTime = cursor.getLong(cursor.getColumnIndex(Places.ENTRY_TIME));
        place.registerTime = cursor.getLong(cursor.getColumnIndex(Places.REGISTER_TIME));

        place.wifis = getWifiFingerprint(placeId);
        place.areas = getGpsCircleArea(placeId);

        if (place.areas == null || place.areas.size() == 0)
            place.areas.add(getPlacePositionEstimate(place.placeId));

        cursor.close();//from   w  w  w  . j a  v  a  2  s .  c  o m
        return place;
    }

    cursor.close();
    return null;
}

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

/**
 * /*from ww w .j  a v a 2 s . c  om*/
 * @param start
 * @param end
 * @param filter
 * @return
 */
public ArrayList<LociLocation> getTrack(long start, long end, int filter) {
    String[] columns = new String[] { Tracks._ID, Tracks.TIME, Tracks.LATITUDE, Tracks.LONGITUDE,
            Tracks.ALTITUDE, Tracks.SPEED, Tracks.BEARING, Tracks.ACCURACY };
    String selection = Tracks.TIME + ">=" + start + " AND " + Tracks.TIME + " <= " + end;

    final SQLiteDatabase db = mDbHelper.getWritableDatabase();
    Cursor cursor = db.query(Tables.TRACKS, columns, selection, null, null, null, null);

    ArrayList<LociLocation> track = new ArrayList<LociLocation>();

    if (cursor.moveToFirst()) {
        do {
            LociLocation loc = new LociLocation(LocationManager.GPS_PROVIDER);
            loc.setTime(cursor.getLong(cursor.getColumnIndex(Tracks.TIME)));
            loc.setLatitude(cursor.getDouble(cursor.getColumnIndex(Tracks.LATITUDE)));
            loc.setLongitude(cursor.getDouble(cursor.getColumnIndex(Tracks.LONGITUDE)));
            loc.setAltitude(cursor.getDouble(cursor.getColumnIndex(Tracks.ALTITUDE)));
            loc.setSpeed(cursor.getFloat(cursor.getColumnIndex(Tracks.SPEED)));
            loc.setBearing(cursor.getFloat(cursor.getColumnIndex(Tracks.BEARING)));
            loc.setAccuracy(cursor.getFloat(cursor.getColumnIndex(Tracks.ACCURACY)));

            track.add(loc);
        } while (cursor.moveToNext());
    }
    cursor.close();
    return track;

}

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

public LociLocation getPlaceLocationEstimationWithBestAccuracy(long start, long end) {
    LociLocation placeLoc = null;//from w  w w. j ava  2 s  .  co  m

    String[] columns = new String[] { Tracks._ID, Tracks.TIME, Tracks.LATITUDE, Tracks.LONGITUDE,
            Tracks.ALTITUDE, Tracks.SPEED, Tracks.BEARING, Tracks.ACCURACY };

    String selection = Tracks.TIME + ">=" + start + " AND " + Tracks.TIME + " <= " + end;

    final SQLiteDatabase db = mDbHelper.getWritableDatabase();
    Cursor cursor = db.query(Tables.TRACKS, columns, selection, null, null, null, null);

    float minAccuracy = Float.MAX_VALUE;

    if (cursor.moveToFirst()) {
        do {
            if (minAccuracy > cursor.getFloat(cursor.getColumnIndex(Tracks.ACCURACY))) {
                if (placeLoc == null)
                    placeLoc = new LociLocation(LocationManager.GPS_PROVIDER);
                placeLoc.setTime(cursor.getLong(cursor.getColumnIndex(Tracks.TIME)));
                placeLoc.setLatitude(cursor.getDouble(cursor.getColumnIndex(Tracks.LATITUDE)));
                placeLoc.setLongitude(cursor.getDouble(cursor.getColumnIndex(Tracks.LONGITUDE)));
                placeLoc.setAltitude(cursor.getDouble(cursor.getColumnIndex(Tracks.ALTITUDE)));
                placeLoc.setSpeed(cursor.getFloat(cursor.getColumnIndex(Tracks.SPEED)));
                placeLoc.setBearing(cursor.getFloat(cursor.getColumnIndex(Tracks.BEARING)));
                placeLoc.setAccuracy(cursor.getFloat(cursor.getColumnIndex(Tracks.ACCURACY)));
                minAccuracy = placeLoc.getAccuracy();
            }
        } while (cursor.moveToNext());
    }
    cursor.close();
    return placeLoc;
}

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

/**
 * Returns an average position fix/*  w  w  w .  j ava2  s  . com*/
 * @param start beginning time
 * @param end ending time
 * @param accuracy filter position fixes with accuracy value higher than this value
 * @return average position within the provided time interval, filtered by accuracy
 */
public LociLocation getAveragePosition(long start, long end, int accuracy) {

    LociLocation placeLoc = null;

    String[] columns = new String[] { Tracks._ID, Tracks.TIME, Tracks.LATITUDE, Tracks.LONGITUDE,
            Tracks.ALTITUDE, Tracks.SPEED, Tracks.BEARING, Tracks.ACCURACY };

    String selection = Tracks.TIME + ">=" + start + " AND " + Tracks.TIME + " <= " + end;

    final SQLiteDatabase db = mDbHelper.getWritableDatabase();
    Cursor cursor = db.query(Tables.TRACKS, columns, selection, null, null, null, null);

    ArrayList<LociLocation> track = new ArrayList<LociLocation>();

    MyLog.d(LociConfig.D.DB.UTILS, TAG,
            String.format("[DB] average position between %s-%s ==> #pos=%d (accuracy=%d)",
                    MyDateUtils.getTimeFormatLong(start), MyDateUtils.getTimeFormatLong(end), cursor.getCount(),
                    accuracy));

    if (cursor.moveToFirst()) {
        do {
            LociLocation loc = new LociLocation(LocationManager.GPS_PROVIDER);
            loc.setTime(cursor.getLong(cursor.getColumnIndex(Tracks.TIME)));
            loc.setLatitude(cursor.getDouble(cursor.getColumnIndex(Tracks.LATITUDE)));
            loc.setLongitude(cursor.getDouble(cursor.getColumnIndex(Tracks.LONGITUDE)));
            loc.setAltitude(cursor.getDouble(cursor.getColumnIndex(Tracks.ALTITUDE)));
            loc.setSpeed(cursor.getFloat(cursor.getColumnIndex(Tracks.SPEED)));
            loc.setBearing(cursor.getFloat(cursor.getColumnIndex(Tracks.BEARING)));
            loc.setAccuracy(cursor.getFloat(cursor.getColumnIndex(Tracks.ACCURACY)));
            track.add(loc);
        } while (cursor.moveToNext());

        placeLoc = LociLocation.averageLocation(track, accuracy);
    }
    cursor.close();
    return placeLoc;

}

From source file:com.openerp.orm.ORM.java

/**
 * Execute query./*  ww w.j a v a  2  s. co  m*/
 * 
 * @param dbHelper
 *            the db helper
 * @param where
 *            the where
 * @return the list
 */
private List<HashMap<String, Object>> executeQuery(BaseDBHelper dbHelper, String where) {
    List<String> cols = new ArrayList<String>();
    for (Fields col : dbHelper.getColumns()) {
        if (!(col.getType() instanceof Many2Many)) {
            cols.add(col.getName());
        }
    }
    String columns[] = cols.toArray(new String[cols.size()]);
    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.query(modelToTable(dbHelper.getModelName()), columns, where, null, null, null, null);
    List<HashMap<String, Object>> data = getResult(dbHelper, null, cursor);
    db.close();
    cursor.close();
    return data;
}