List of usage examples for android.database.sqlite SQLiteDatabase query
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy,
String having, String orderBy, String limit)
From source file:com.spoiledmilk.ibikecph.util.DB.java
public SearchListItem getFavoriteByName(String name) { SearchListItem ret = null;/*from w w w .j av a 2 s. com*/ SQLiteDatabase db = getReadableDatabase(); if (db == null) return null; String[] columns = { KEY_ID, KEY_NAME, KEY_ADDRESS, KEY_SOURCE, KEY_SUBSOURCE, KEY_LAT, KEY_LONG, KEY_API_ID }; Cursor cursor = db.query(TABLE_FAVORITES, columns, KEY_NAME + " = ? ", new String[] { name.trim() }, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { while (cursor != null && !cursor.isAfterLast()) { int colId = cursor.getColumnIndex(KEY_ID); int colName = cursor.getColumnIndex(KEY_NAME); int colAddress = cursor.getColumnIndex(KEY_ADDRESS); int colSubSource = cursor.getColumnIndex(KEY_SUBSOURCE); int colLat = cursor.getColumnIndex(KEY_LAT); int colLong = cursor.getColumnIndex(KEY_LONG); int colApiId = cursor.getColumnIndex(KEY_API_ID); ret = new FavoritesData(cursor.getInt(colId), cursor.getString(colName), cursor.getString(colAddress), cursor.getString(colSubSource), cursor.getDouble(colLat), cursor.getDouble(colLong), cursor.getInt(colApiId)); break; } } if (cursor != null) cursor.close(); db.close(); return ret; }
From source file:com.spoiledmilk.ibikecph.util.DB.java
public ArrayList<SearchListItem> getFavorites2() { ArrayList<SearchListItem> ret = new ArrayList<SearchListItem>(); SQLiteDatabase db = getReadableDatabase(); if (db == null) return null; String[] columns = { KEY_ID, KEY_NAME, KEY_ADDRESS, KEY_SOURCE, KEY_SUBSOURCE, KEY_LAT, KEY_LONG, KEY_API_ID };// w ww. java 2s .c o m Cursor cursor = db.query(TABLE_FAVORITES, columns, null, null, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { while (cursor != null && !cursor.isAfterLast()) { int colId = cursor.getColumnIndex(KEY_ID); int colName = cursor.getColumnIndex(KEY_NAME); int colAddress = cursor.getColumnIndex(KEY_ADDRESS); int colSubSource = cursor.getColumnIndex(KEY_SUBSOURCE); int colLat = cursor.getColumnIndex(KEY_LAT); int colLong = cursor.getColumnIndex(KEY_LONG); int colApiId = cursor.getColumnIndex(KEY_API_ID); FavoritesData fd = new FavoritesData(cursor.getInt(colId), cursor.getString(colName), cursor.getString(colAddress), cursor.getString(colSubSource), cursor.getDouble(colLat), cursor.getDouble(colLong), cursor.getInt(colApiId)); ret.add((SearchListItem) fd); cursor.moveToNext(); } } if (cursor != null) cursor.close(); db.close(); return ret; }
From source file:com.spoiledmilk.ibikecph.util.DB.java
public ArrayList<SearchListItem> getFavoritesForString(String srchString) { ArrayList<SearchListItem> ret = new ArrayList<SearchListItem>(); SQLiteDatabase db = getReadableDatabase(); if (db == null) return null; String[] columns = { KEY_ID, KEY_NAME, KEY_ADDRESS, KEY_SOURCE, KEY_SUBSOURCE, KEY_LAT, KEY_LONG, KEY_API_ID };//from w ww .j a v a2 s . c o m Cursor cursor = db.query(TABLE_FAVORITES, columns, KEY_NAME + " LIKE ? ", new String[] { "%" + srchString + "%" }, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { while (cursor != null && !cursor.isAfterLast()) { int colId = cursor.getColumnIndex(KEY_ID); int colName = cursor.getColumnIndex(KEY_NAME); int colAddress = cursor.getColumnIndex(KEY_ADDRESS); int colSubSource = cursor.getColumnIndex(KEY_SUBSOURCE); int colLat = cursor.getColumnIndex(KEY_LAT); int colLong = cursor.getColumnIndex(KEY_LONG); int colApiId = cursor.getColumnIndex(KEY_API_ID); FavoritesData fd = new FavoritesData(cursor.getInt(colId), cursor.getString(colName), cursor.getString(colAddress), cursor.getString(colSubSource), cursor.getDouble(colLat), cursor.getDouble(colLong), cursor.getInt(colApiId)); ret.add((SearchListItem) fd); cursor.moveToNext(); } } if (cursor != null) cursor.close(); db.close(); return ret; }
From source file:com.spoiledmilk.ibikecph.util.DB.java
public ArrayList<FavoritesData> getFavorites(ArrayList<FavoritesData> ret) { if (ret == null) { ret = new ArrayList<FavoritesData>(); } else {//from w ww .j a v a2s . c om ret.clear(); } SQLiteDatabase db = getReadableDatabase(); if (db == null) { return null; } String[] columns = { KEY_ID, KEY_NAME, KEY_ADDRESS, KEY_SOURCE, KEY_SUBSOURCE, KEY_LAT, KEY_LONG, KEY_API_ID }; Cursor cursor = db.query(TABLE_FAVORITES, columns, null, null, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { while (cursor != null && !cursor.isAfterLast()) { int colId = cursor.getColumnIndex(KEY_ID); int colName = cursor.getColumnIndex(KEY_NAME); int colAddress = cursor.getColumnIndex(KEY_ADDRESS); int colSubSource = cursor.getColumnIndex(KEY_SUBSOURCE); int colLat = cursor.getColumnIndex(KEY_LAT); int colLong = cursor.getColumnIndex(KEY_LONG); int colApiId = cursor.getColumnIndex(KEY_API_ID); FavoritesData fd = new FavoritesData(cursor.getInt(colId), cursor.getString(colName), cursor.getString(colAddress), cursor.getString(colSubSource), cursor.getDouble(colLat), cursor.getDouble(colLong), cursor.getInt(colApiId)); ret.add(fd); cursor.moveToNext(); } } if (cursor != null) { cursor.close(); } db.close(); LOG.d("favourites count from DB = " + ret.size()); return ret; }
From source file:com.spoiledmilk.ibikecph.util.DB.java
public ArrayList<SearchListItem> getSearchHistoryForString(String srchString) { ArrayList<SearchListItem> ret = new ArrayList<SearchListItem>(); SQLiteDatabase db = getReadableDatabase(); if (db == null) return null; String[] columns = { KEY_ID, KEY_NAME, KEY_ADDRESS, KEY_START_DATE, KEY_END_DATE, KEY_SOURCE, KEY_SUBSOURCE, KEY_LAT, KEY_LONG };/*from w w w .j av a 2s. com*/ Cursor cursor = db.query(TABLE_SEARCH_HISTORY, columns, KEY_NAME + " LIKE ? OR " + KEY_ADDRESS + " LIKE ?", new String[] { "%" + srchString + "%", "%" + srchString + "%" }, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { while (cursor != null && !cursor.isAfterLast()) { int colId = cursor.getColumnIndex(KEY_ID); int colName = cursor.getColumnIndex(KEY_NAME); int colAddress = cursor.getColumnIndex(KEY_ADDRESS); int colStartDate = cursor.getColumnIndex(KEY_START_DATE); int colEndDate = cursor.getColumnIndex(KEY_END_DATE); int colSource = cursor.getColumnIndex(KEY_SOURCE); int colSubSource = cursor.getColumnIndex(KEY_SUBSOURCE); int colLat = cursor.getColumnIndex(KEY_LAT); int colLong = cursor.getColumnIndex(KEY_LONG); HistoryData hd = new HistoryData(cursor.getInt(colId), cursor.getString(colName), cursor.getString(colAddress), cursor.getString(colStartDate), cursor.getString(colEndDate), cursor.getString(colSource), cursor.getString(colSubSource), cursor.getDouble(colLat), cursor.getDouble(colLong)); if (hd.getName() != null && !hd.getName().trim().equals("")) ret.add(hd); cursor.moveToNext(); } } if (cursor != null) cursor.close(); db.close(); return ret; }
From source file:com.spoiledmilk.ibikecph.util.DB.java
public SearchListItem getSearchHistoryByName(String name) { SearchListItem ret = null;//w w w . ja va 2 s. c o m SQLiteDatabase db = getReadableDatabase(); if (db == null) return null; String[] columns = { KEY_ID, KEY_NAME, KEY_ADDRESS, KEY_START_DATE, KEY_END_DATE, KEY_SOURCE, KEY_SUBSOURCE, KEY_LAT, KEY_LONG }; Cursor cursor = db.query(TABLE_SEARCH_HISTORY, columns, KEY_NAME + " = ? ", new String[] { name.trim() }, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { while (cursor != null && !cursor.isAfterLast()) { int colId = cursor.getColumnIndex(KEY_ID); int colName = cursor.getColumnIndex(KEY_NAME); int colAddress = cursor.getColumnIndex(KEY_ADDRESS); int colStartDate = cursor.getColumnIndex(KEY_START_DATE); int colEndDate = cursor.getColumnIndex(KEY_END_DATE); int colSource = cursor.getColumnIndex(KEY_SOURCE); int colSubSource = cursor.getColumnIndex(KEY_SUBSOURCE); int colLat = cursor.getColumnIndex(KEY_LAT); int colLong = cursor.getColumnIndex(KEY_LONG); HistoryData hd = new HistoryData(cursor.getInt(colId), cursor.getString(colName), cursor.getString(colAddress), cursor.getString(colStartDate), cursor.getString(colEndDate), cursor.getString(colSource), cursor.getString(colSubSource), cursor.getDouble(colLat), cursor.getDouble(colLong)); if (hd.getName() != null && !hd.getName().trim().equals("")) { ret = hd; } break; } } if (cursor != null) cursor.close(); db.close(); return ret; }
From source file:com.spoiledmilk.ibikecph.util.DB.java
public ArrayList<SearchListItem> getSearchHistory() { ArrayList<SearchListItem> ret = new ArrayList<SearchListItem>(); SQLiteDatabase db = getReadableDatabase(); if (db == null) return null; String[] columns = { KEY_ID, KEY_NAME, KEY_ADDRESS, KEY_START_DATE, KEY_END_DATE, KEY_SOURCE, KEY_SUBSOURCE, KEY_LAT, KEY_LONG };/*from www.j a v a 2s. c o m*/ Cursor cursor = db.query(TABLE_SEARCH_HISTORY, columns, null, null, null, null, KEY_START_DATE + " DESC", null); if (cursor != null && cursor.moveToFirst()) { while (cursor != null && !cursor.isAfterLast()) { int colId = cursor.getColumnIndex(KEY_ID); int colName = cursor.getColumnIndex(KEY_NAME); int colAddress = cursor.getColumnIndex(KEY_ADDRESS); int colStartDate = cursor.getColumnIndex(KEY_START_DATE); int colEndDate = cursor.getColumnIndex(KEY_END_DATE); int colSource = cursor.getColumnIndex(KEY_SOURCE); int colSubSource = cursor.getColumnIndex(KEY_SUBSOURCE); int colLat = cursor.getColumnIndex(KEY_LAT); int colLong = cursor.getColumnIndex(KEY_LONG); HistoryData hd = new HistoryData(cursor.getInt(colId), cursor.getString(colName), cursor.getString(colAddress), cursor.getString(colStartDate), cursor.getString(colEndDate), cursor.getString(colSource), cursor.getString(colSubSource), cursor.getDouble(colLat), cursor.getDouble(colLong)); if (hd.getName() != null && !hd.getName().trim().equals("")) { ret.add(hd); } if (ret.size() > 10) { break; } cursor.moveToNext(); } } if (cursor != null) cursor.close(); db.close(); return ret; }
From source file:net.potterpcs.recipebook.RecipeData.java
public String getOldestCacheEntry() { synchronized (DB_LOCK) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c = db.query(CACHE_TABLE, CACHE_FIELDS, null, null, null, null, CT_CACHED, "1"); if (c.getCount() > 0) { c.moveToFirst();/*from w w w . j av a 2 s. c o m*/ String s = c.getString(c.getColumnIndex(CT_URI)); c.close(); return s; } else { c.close(); return null; } } }
From source file:android.melbournehistorymap.MapsActivity.java
/** * Manipulates the map once available./*from w w w. ja va 2s. c o m*/ * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; spinner = (ProgressBar) findViewById(R.id.prograssSpinner); //check if permission has been granted if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Permission has already been granted return; } mMap.setMyLocationEnabled(true); mMap.getUiSettings().setZoomControlsEnabled(false); double lat; double lng; final int radius; int zoom; lat = Double.parseDouble(CurrLat); lng = Double.parseDouble(CurrLong); //build current location LatLng currentLocation = new LatLng(lat, lng); final LatLng realLocation = currentLocation; if (MELBOURNE.contains(currentLocation)) { mMap.getUiSettings().setMyLocationButtonEnabled(true); zoom = 17; } else { mMap.getUiSettings().setMyLocationButtonEnabled(false); lat = -37.81161508043379; lng = 144.9647320434451; zoom = 15; currentLocation = new LatLng(lat, lng); } mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 13)); CameraPosition cameraPosition = new CameraPosition.Builder().target(currentLocation) // Sets the center of the map to location user .zoom(zoom) // Sets the zoom .bearing(0) // Sets the orientation of the camera to east .tilt(25) // Sets the tilt of the camera to 30 degrees .build(); // Creates a CameraPosition from the builder //Animate user to map location, if in Melbourne or outside of Melbourne bounds mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), new GoogleMap.CancelableCallback() { @Override public void onFinish() { updateMap(); } @Override public void onCancel() { } }); final TextView placeTitle = (TextView) findViewById(R.id.placeTitle); final TextView placeVic = (TextView) findViewById(R.id.placeVic); final TextView expPlaceTitle = (TextView) findViewById(R.id.expPlaceTitle); final TextView expPlaceVic = (TextView) findViewById(R.id.expPlaceVic); final TextView expPlaceDescription = (TextView) findViewById(R.id.placeDescription); final TextView wikiLicense = (TextView) findViewById(R.id.wikiLicense); final TextView expPlaceDistance = (TextView) findViewById(R.id.expPlaceDistance); final RelativeLayout tile = (RelativeLayout) findViewById(R.id.tile); final TextView fab = (TextView) findViewById(R.id.fab); final RelativeLayout distanceCont = (RelativeLayout) findViewById(R.id.distanceContainer); // String license = "Text is available under the <a rel=\"license\" href=\"//en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License\">Creative Commons Attribution-ShareAlike License</a><a rel=\"license\" href=\"//creativecommons.org/licenses/by-sa/3.0/\" style=\"display:none;\"></a>;\n" + // "additional terms may apply."; // wikiLicense.setText(Html.fromHtml(license)); // wikiLicense.setMovementMethod(LinkMovementMethod.getInstance()); //Marker click mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { String title = marker.getTitle(); mMap.setPadding(0, 0, 0, 620); //set clicked marker to full opacity marker.setAlpha(1f); //set previous marker back to partial opac (if there is a prevMarker if (dirtyMarker == 1) { prevMarker.setAlpha(0.6f); } prevMarker = marker; dirtyMarker = 1; //Set DB helper DBHelper myDBHelper = new DBHelper(MapsActivity.this, WikiAPI.DB_NAME, null, WikiAPI.VERSION); //Only search for Wiki API requests if no place article returned. // ** //Open DB as readable only. SQLiteDatabase db = myDBHelper.getReadableDatabase(); //Set the query String dbFriendlyName = title.replace("\'", "\'\'"); //Limit by 1 rows Cursor cursor = db.query(DBHelper.TABLE_NAME, null, "PLACE_NAME = '" + dbFriendlyName + "'", null, null, null, null, "1"); //move through each row returned in the query results while (cursor.moveToNext()) { String place_ID = cursor.getString(cursor.getColumnIndex("PLACE_ID")); String placeName = cursor.getString(cursor.getColumnIndex("PLACE_NAME")); String placeLoc = cursor.getString(cursor.getColumnIndex("PLACE_LOCATION")); String placeArticle = cursor.getString(cursor.getColumnIndex("ARTICLE")); String placeLat = cursor.getString(cursor.getColumnIndex("LAT")); String placeLng = cursor.getString(cursor.getColumnIndex("LNG")); //Get Google Place photos //Source: https://developers.google.com/places/android-api/photos final String placeId = place_ID; Places.GeoDataApi.getPlacePhotos(mGoogleApiClient, placeId) .setResultCallback(new ResultCallback<PlacePhotoMetadataResult>() { @Override public void onResult(PlacePhotoMetadataResult photos) { if (!photos.getStatus().isSuccess()) { return; } ImageView mImageView = (ImageView) findViewById(R.id.imageView); ImageView mImageViewExpanded = (ImageView) findViewById(R.id.headerImage); TextView txtAttribute = (TextView) findViewById(R.id.photoAttribute); TextView expTxtAttribute = (TextView) findViewById(R.id.expPhotoAttribute); PlacePhotoMetadataBuffer photoMetadataBuffer = photos.getPhotoMetadata(); if (photoMetadataBuffer.getCount() > 0) { // Display the first bitmap in an ImageView in the size of the view photoMetadataBuffer.get(0).getScaledPhoto(mGoogleApiClient, 600, 200) .setResultCallback(mDisplayPhotoResultCallback); //get photo attributions PlacePhotoMetadata photo = photoMetadataBuffer.get(0); CharSequence attribution = photo.getAttributions(); if (attribution != null) { txtAttribute.setText(Html.fromHtml(String.valueOf(attribution))); expTxtAttribute.setText(Html.fromHtml(String.valueOf(attribution))); //http://stackoverflow.com/questions/4303160/how-can-i-make-links-in-fromhtml-clickable-android txtAttribute.setMovementMethod(LinkMovementMethod.getInstance()); expTxtAttribute.setMovementMethod(LinkMovementMethod.getInstance()); } else { txtAttribute.setText(" "); expTxtAttribute.setText(" "); } } else { //Reset image view as no photo was identified mImageView.setImageResource(android.R.color.transparent); mImageViewExpanded.setImageResource(android.R.color.transparent); txtAttribute.setText(R.string.no_photos); expTxtAttribute.setText(R.string.no_photos); } photoMetadataBuffer.release(); } }); LatLng destLocation = new LatLng(Double.parseDouble(placeLat), Double.parseDouble(placeLng)); //Work out distance between current location and place location //Source Library: https://github.com/googlemaps/android-maps-utils double distance = SphericalUtil.computeDistanceBetween(realLocation, destLocation); distance = Math.round(distance); distance = distance * 0.001; String strDistance = String.valueOf(distance); String[] arrDistance = strDistance.split("\\."); String unit = "km"; if (arrDistance[0] == "0") { unit = "m"; strDistance = arrDistance[1]; } else { strDistance = arrDistance[0] + "." + arrDistance[1].substring(0, 1); } placeArticle = placeArticle .replaceAll("(<div class=\"thumb t).*\\s.*\\s.*\\s.*\\s.*\\s<\\/div>\\s<\\/div>", " "); Spannable noUnderlineMessage = new SpannableString(Html.fromHtml(placeArticle)); //http://stackoverflow.com/questions/4096851/remove-underline-from-links-in-textview-android for (URLSpan u : noUnderlineMessage.getSpans(0, noUnderlineMessage.length(), URLSpan.class)) { noUnderlineMessage.setSpan(new UnderlineSpan() { public void updateDrawState(TextPaint tp) { tp.setUnderlineText(false); } }, noUnderlineMessage.getSpanStart(u), noUnderlineMessage.getSpanEnd(u), 0); } placeArticle = String.valueOf(noUnderlineMessage); placeArticle = placeArticle.replaceAll("(\\[\\d\\])", " "); placeTitle.setText(placeName); expPlaceTitle.setText(placeName); placeVic.setText(placeLoc); expPlaceVic.setText(placeLoc); expPlaceDescription.setText(placeArticle); if (MELBOURNE.contains(realLocation)) { expPlaceDistance.setText("Distance: " + strDistance + unit); distanceCont.setVisibility(View.VISIBLE); } } tile.setVisibility(View.VISIBLE); fab.setVisibility(View.VISIBLE); //Set to true to not show default behaviour. return false; } }); mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng latLng) { int viewStatus = tile.getVisibility(); if (viewStatus == View.VISIBLE) { tile.setVisibility(View.INVISIBLE); fab.setVisibility(View.INVISIBLE); //set previous marker back to partial opac (if there is a prevMarker if (dirtyMarker == 1) { prevMarker.setAlpha(0.6f); } } mMap.setPadding(0, 0, 0, 0); } }); FloatingActionButton shareIcon = (FloatingActionButton) findViewById(R.id.shareIcon); shareIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //https://www.google.com.au/maps/place/Federation+Square/@-37.8179789,144.9668635,15z //Build implicit intent by triggering a SENDTO action, which will capture applications that allow for messages //that allow for messages to be sent to a specific user with data //http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); //Build SMS String encodedPlace = "empty"; try { encodedPlace = URLEncoder.encode(String.valueOf(expPlaceTitle.getText()), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(expPlaceTitle.getText()) + " \n\nhttps://www.google.com.au/maps/place/" + encodedPlace); Intent sms = Intent.createChooser(intent, null); startActivity(sms); } }); TextView fullArticle = (TextView) findViewById(R.id.fullArticle); fullArticle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String wikiPlace = WikiPlace.getName(String.valueOf(expPlaceTitle.getText())); String url = "https://en.wikipedia.org/wiki/" + wikiPlace; Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browserIntent); } }); }
From source file:org.frc836.database.DB.java
public long getWheelBaseIDFromName(String base, SQLiteDatabase db) { String[] projection = { WHEEL_BASE_LU_Entry.COLUMN_NAME_ID }; String[] where = { base };/*from www. jav a 2 s .c o m*/ Cursor c = db.query(WHEEL_BASE_LU_Entry.TABLE_NAME, projection, // select WHEEL_BASE_LU_Entry.COLUMN_NAME_WHEEL_BASE_DESC + " LIKE ?", where, // EventName null, // don't group null, // don't filter null, // don't order "0,1"); // limit to 1 long ret = -1; try { c.moveToFirst(); ret = c.getLong(c.getColumnIndexOrThrow(WHEEL_BASE_LU_Entry.COLUMN_NAME_ID)); } finally { if (c != null) c.close(); } return ret; }