Sqlite Android User Defined Function Issue. "no such function"

1.8k Views Asked by At

Tried to integrate mathematical function in sqlite for distance calculation. Did the following but still getting the error no such function: cos.

sql_trig.c

#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1;
#include <stdlib.h>

/* this bit is required to get M_PI out of MS headers */
#if defined( _WIN32 )
#define _USE_MATH_DEFINES
#endif /* _WIN32 */

#include <math.h>

#define RADIANS(d) (( d / 180.0 ) * M_PI)

static void sql_trig_cos( sqlite3_context *ctx, int num_values, sqlite3_value **values )
{
    double a = RADIANS(sqlite3_value_double( values[0] ));
    sqlite3_result_double( ctx, cos( a ) );
}

int sqlite3_extension_init( sqlite3 *db, char **error, const sqlite3_api_routines *api )
{
    SQLITE_EXTENSION_INIT2(api);

    sqlite3_create_function( db, "cos", 1, SQLITE_UTF8, NULL, &sql_trig_cos, NULL, NULL );

    return SQLITE_OK;
}

Android.mk

#LOCAL_PATH is used to locate source files in the development tree.
#the macro my-dir provided by the build system, indicates the path of the current directory
LOCAL_PATH:=$(call my-dir)

#####################################################################
#            build sqlite3                                            #
#####################################################################
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_MODULE:=sqlite3
LOCAL_SRC_FILES:=sqlite3.c
#include $(BUILD_STATIC_LIBRARY)
include $(BUILD_SHARED_LIBRARY)


#####################################################################
#            build our code                    #
#####################################################################
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_MODULE:=sql_trig
LOCAL_SRC_FILES:=sql_trig.c
#LOCAL_STATIC_LIBRARIES:=libsqlite3
LOCAL_SHARED_LIBRARIES:=libsqlite3
LOCAL_LDLIBS:=-llog -lm
include $(BUILD_SHARED_LIBRARY)
#include $(BUILD_EXECUTABLE)

Build libsql_trig.so and libsql3.so successfully.

Loaded the library

static {
    try
    {
        System.loadLibrary("sqlite3");
        System.loadLibrary("sql_trig");
    } catch (UnsatisfiedLinkError e)
    {
        Log.e("Error", e.getMessage());
    }
}

When on calling it from database as below:

String query = "SELECT cos(21) AS distance";
Cursor cursor = database.rawQuery(query, null);

Got the error

FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: no such function: cos (code 1): , while compiling: SELECT cos(21) AS distance
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1436)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1375)
at com.aarogya.database.DbManager.checkDistanceFormula(DbManager.java:79)

Any ideas / suggestions would be helpful.

Thanks.

0

There are 0 best solutions below