Database Query on Android

2

I'd like some help from you to check out what's wrong with this query:

        int raioKm = 10;

        String latitude =
                "CONVERT("                                                          +
                    "NUMERIC(10,7), "                                               +
                    "SUBSTRING(localizacao, 0, CHARINDEX(localizacao, ',' - 1))"    +
                ")";
        String longitude =
                "CONVERT("                                                      +
                    "NUMERIC(10,7), "                                           +
                    "SUBSTRING(localizacao, CHARINDEX(localizacao, ',' + 1), "  +
                        "LEN(localizacao))"                                     +
                ")";

        String formulaHaversine = "(6371 * ACOS(" +
            "COS(radians("+ data.getDoubleExtra("latitude", 0.00) +")) * "      +
            "COS(radians(" + latitude + ")) * "                                 +
            "COS(radians("+ data.getDoubleExtra("longitude", 0.00) +") - "      +
            "RADIANS("+ longitude + ")) + "                                     +
            "SIN(radians(-19.83996)) * "                                        +
            "SIN(radians(" + latitude + "))"                                    +
        "))";

        System.out.println(latitude + ", " + longitude);

        String sql =    "SELECT idFoto as _id, localizacao, diretorio, descricao, "   +
            formulaHaversine + " AS distance FROM Foto HAVING distance <= " + raioKm;

INTRODUCTION OF THE SUBJECT

I'm trying to bring from the database only the rows of my table that are within a 10-kilometer radius from a reference point given by the expression data.getDoubleExtra () that returns latitude or the length of this point.

The location column of the Photo table is formatted as follows: "latitude, longitude" (with spaces before and after the comma) - and this column that needs to be reformatted so that its values , of type STRING , can be converted to DOUBLE . For this purpose, I use the CONVERT function, along with other functions, to get the latitude and longitude and thus use them in STRING sql .

To determine if the coordinates are within the given radius, use the Haversine formula ( Identify if coordinate set is within a radius on Android ). Then, the formula is used to generate the STRING sql "final."

PROBLEM

As I said, this project is in Android . Therefore, this database query is being done within the onActivityResult method and when I try to compile it I get the following error:

  

java.lang.RuntimeException: Failure delivering result   ResultInfo {who = null, request = 2, result = -1, data = Intent {   cmp = gborniam.bbphoto / .grafica.MapsActivity (has extras)}} to activity   {gborniam.bbphoto / gborniam.bbphoto.grafica.Principal}:   android.database.sqlite.SQLiteException: no such function: ACOS (code   1):, while compiling: SELECT idFoto as _id, location, directory,   (6371 * ACOS (COS (radians (-17.478109860099323)) *   COS (radians (CONVERT (NUMERIC (10,7), SUBSTRING (location, 0,   CHARINDEX (location, ',' - 1))))) * COS (radians (-49.62723668664694)   - RADIANS (CONVERT (NUMERIC (10,7), SUBSTRING (location, CHARINDEX (location, ',' + 1), LEN (location))))) +   SIN (radians (-19.83996)) * SIN (radians (CONVERT (NUMERIC (10,7),   SUBSTRING (location, 0, CHARINDEX (location, ',' - 1)))))))) AS   distance FROM Photo HAVING distance

asked by anonymous 16.06.2017 / 01:12

1 answer

3

For the error you are giving and for what I just read , it seems the same is occurring because SQLite does not support function ACOS , in addition to others that are contained in query .

See the most relevant part of the exception:

  

android.database.sqlite.SQLiteException: no such function: ACOS (code   1):

There is a short here section that confirms the existence of other alternatives created by community contributors, but they are options that do not are part of the standard SQLite package, which should be used by " rich developer account ", they claim, by not having an official check.

See some similar problems below:

One suggestion maybe, say perhaps, more feasible for this situation would be to use the Haversine formula in Java to calculate the distance between the points. See a Github example .

    
16.06.2017 / 01:55