i need to create a function for sqlite, because the sin and cos mysql-function doesn't exist, and i need to make a query like:
String q = "SELECT * FROM shops WHERE _id > 0 AND distance(lat, lng, latitudine, longitudine) < 20 AND cancellato=0 ORDER BY _id";
Cursor c = myDataBase.rawQuery(q, null);
return c;
so, i need a function for distance.
and i can't do it via code because it's a list of +8000 shops!
how can i? i didn't found anything on the web.
thanks!
distance(lat, lng, latitudine, longitudine)
I think you have to calculate distance pragmatically from your javacode then you can compare
http://www.sqlite.org/lang_aggfunc.html
http://oreilly.com/catalog/sqlnut/chapter/ch04.html
Related
I would like to search places between two given distances, using GeoPoints. Using the currently api, I think I could make something like this pseudo algorithm:
query.whereWithinKilometers("directions", GlobalData.ownerGeoPoint, MAXIMUM distance);
MINUS
query.whereWithinKilometers("directions", GlobalData.ownerGeoPoint, MINIMUM distance);
How can I translate to real code?
Finally I found a way to do it with a parse query.
// Do not want locations further than maxDistance
ParseQuery query = ParseQuery.getQuery("MyData");
query.whereWithinKilometers("location", userGeoPoint, maxDistance);
// Do not want locations closer than minDistance
ParseQuery<ParseObject> innerQuery = ParseQuery.getQuery("MyData");
innerQuery.whereWithinKilometers("location", userGeoPoint, minDistance);
query.whereDoesNotMatchKeyInQuery("objectId", "objectId", innerQuery);
Likely you'll have to pull in everything within the maximum distance from Parse to your app, then inside your app filter out anything that is closer than the minimum.
Thankfully Parse includes some distance measuring as part of PFGeoPoint. Check out the three methods here.
distanceInRadiansTo:
distanceInMilesTo:
distanceInKilometersTo:
So your pseudocode is:
(After you get back everything within the outer radius)
Make a new array (var) called finalList
For i = 0; i < objects.count; i++ {
var gp: PFGeoPoint = objects[i]
if gp.distanceInMilesTo:originalGeoPoint >= minimumRadiusInMiles {
finalList.push(gp)
}
}
I have this SQL.
select sum(distance) AS distance FROM RoadTravelTableFile where checkBoxBusiness ='1' and plate_Number = 'AAA567'"
I have seen this simple query for raw sql in the Ormlite document.
long maxUnits = orderDao.queryRawValue("select max(units) from orders");
With that example, I coded my sql like this and it works.
distance = (int) getHelper().getRoadTravelTableFileIntegerDao().queryRawValue("SELECT SUM(distance) FROM RoadTravelTableFile where checkBoxBusiness = '1' and plate_Number ='AAA567' ");
But I have a problem, How can you make the checkBoxBusiness and plate_Number value as a parameter?
Replace current values with ? and add arguments to the queryRawValue method.
Integer checkBoxBusiness = 1;
String plateNumber = "AAA567";
distance = (int) getHelper()
.getRoadTravelTableFileIntegerDao()
.queryRawValue("SELECT SUM(distance) FROM RoadTravelTableFile where checkBoxBusiness = ? and plate_Number = ?", checkBoxBusiness, plateNumber);
I wan't to compare two coordinates in my database, but i can't use the addition or subtraction. The database i use is SQLite, on Android.
Here is my SQL request :
SELECT m.numMag, nomMag, photoMag, URLMag, latitudeMag,
longitudeMag, numEns, numAdr
FROM Mag m, Prix p
WHERE m.numMag=p.numMag AND p.numProduit=1
AND latitude <= SUM(48.453354+0.01)
AND latitude >=SUM(48.453354-0.01)
AND longitude <= SUM(2.306644+0.06)
AND longitude >= SUM(2.306644-0.06);
Of course this request is completely wrong, but it's my last test. Somebody can help me ?
Other information : SUM(generated Float, -/+number has already been set)
SUM is an aggregation function, so it works when you have a GROUP BY. Just remove it . . .
SELECT m.numMag, nomMag, photoMag, URLMag, latitudeMag,
longitudeMag, numEns, numAdr
FROM Mag m, Prix p
WHERE m.numMag=p.numMag AND p.numProduit=1
AND latitude <= (48.453354+0.01)
AND latitude >=(48.453354-0.01)
AND longitude <= (2.306644+0.06)
AND longitude >= (2.306644-0.06);
You should also use proper join syntax and you can simplify using between:
SELECT m.numMag, nomMag, photoMag, URLMag, latitudeMag,
longitudeMag, numEns, numAdr
FROM Mag m join
Prix p
on m.numMag=p.numMag
WHERE p.numProduit=1 and
latitude between (48.453354-0.01) and (48.453354+0.01) and
longitude between (2.306644-0.06) and (2.306644+0.06)
I want to display all my records that are stored in the database
c = db.DBhelper.getChamp1(c.getCount);
//startManagingCursor(c);
int j = 0;
stateNameArray = new String[c.getCount()];
c.moveToFirst();
while(!c.isAfterLast()) {
stateNameArray[j] = c.getString(0);
j++;
Log.i("DEBUG_SQL","" + c.getString(0)+ " "+c.getString(j));
c.moveToNext();
}
//String resultat = ;
Log.i("DEBUG_SQL","" + c.getColumnIndex("article"));
I get an error when I write c.getCount – why? When I write a number like 1 or 2 or 3... that works.
And if I write
c = db.rawQuery("SELECT * FROM loan", null);
I get an error, but if I write
db.rawQuery("SELECT * FROM loan WHERE _id=1", null);
That works. Why?
At the very least, I see a problem with this log statement, where c.getString(j) doesn't make sense. And it may trigger an error as j gets larger.
Log.i("DEBUG_SQL","" + c.getString(0)+ " "+c.getString(j));
What data did you intend to access with the statement c.getString(j)?
On the getCount error. I assumed the error in the following was a typo. But is this where the error associated with getCount was located?
c = db.DBhelper.getChamp1(c.getCount);
But I shouldn't assume - you never know. It should read (add brackets to the method call).
c = db.DBhelper.getChamp1(c.getCount());
And as #Barak mentioned, what is going on with this statement?
To answer the question about your getCount isuue, you get the error because of this:
c = db.DBhelper.getChamp1(c.getCount);
You're trying to get the count of the cursor before you have it (and you're missing ()).
This would work since you have a cursor to count before you pull the next one:
c = db.getSomeCursor;
c1 = db.DBhelper.getChamp1(c.getCount());
Let us know what you're trying to achieve (I can't figure it out from the code you posted) and maybe we can be more helpful.
I know there many solutions available in Stackover flow but looks none have gone to address the basic question I have is
- I have more than 2500 lon/lat data
- I want to store/retrive them from sqlite
- Query nearest locations based on user input.
Looking for optimum solutions
Note: I have gone through
Finding the closest point to a given point
What is this Geohashing all about How can use Geohashing in my this particular problem
Geohashing is an encoding of latitude, longitude pairs such that points in proximity to eaxh other have geohashes with a common prefix. However, this does not work with every coordinate on the planet, i.e. there regions where the goehash changes significantly for points in proximity. Depending on the hashing algorithms the area near to equator may be such an area.
See here for more detail: http://en.wikipedia.org/wiki/Geohash
For a relatively small database of ca. 500 locations I was able to find the nearest location to a given point of reference (the user's location) very fast by searching for points inside an intervall of 0.1 degrees. Here is the code for the query:
/**
* Query the airfields table for airfields near the given position.
* #param dbCon DB connection
* #param ref_lat latitude
* #param ref_lon longitude
* #return Answer the airfield nearest to the given position as array
* of objects: id, designator, latitude, longitude.
* Answer <code>null</code> if their is no airfield near the
* given position plus or minus 0.1 degrees.
*/
private Object[] rangeQuery(final SQLiteDatabase dbCon, final double ref_lat, final double ref_lon) {
if( DEBUG )
Log.d( TAG, "rangeQuery lat=" + ref_lat + ", lon=" + ref_lon);
final SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(AirfieldsTable.TABLE_NAME);
final String[] whereArgs = new String[] {
Double.toString(ref_lat - 0.1d), Double.toString(ref_lat + 0.1d),
Double.toString(ref_lon - 0.1d), Double.toString(ref_lon + 0.1d)
};
final Cursor crsr = qb.query(dbCon, allFields(), AirfieldsTable.RANGE_CLAUSE, whereArgs, null, null, null);
final Object[] val = this.scanForNearest(crsr, ref_lat, ref_lon);
crsr.close();
if( DEBUG )
Log.d( TAG, "scanForNearest returned " + val);
return val;
}
If there is more than one row selected I compare the remaining points directly (thats what scanForNearest() does). Its fast enough to find the airport after the logger (its a logging application) detected a landing.