尝试将数学函数集成到sqlite中进行距离计算。做了以下但仍然收到错误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)
成功构建libsql_trig.so和libsql3.so。
加载了库
static {
try
{
System.loadLibrary("sqlite3");
System.loadLibrary("sql_trig");
} catch (UnsatisfiedLinkError e)
{
Log.e("Error", e.getMessage());
}
}
当从数据库中调用它时,如下所示:
String query = "SELECT cos(21) AS distance";
Cursor cursor = database.rawQuery(query, null);
得到错误
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)
任何想法/建议都会有所帮助。
感谢。