问题描述
我在开发Android的应用程序,我需要创建SQLite中的UDF。是否有可能在sqlite的创建?如果是如何做到这一点?
I am developing an application in android and I need to create an udf in sqlite. Is it possible to create it in sqlite? And if yes how to do this?
推荐答案
SQLite没有在甲骨文或MS SQL Server的方式做用户定义函数的支持。对于SQLite的,您必须创建在C / C ++的回调函数,并使用 sqlite3_create_function 调用挂钩函数了。
SQLite does not have support for user-defined functions in the way that Oracle or MS SQL Server does. For SQLite, you must create a callback function in C/C++ and hook the function up using the sqlite3_create_function call.
不幸的是,Android的SQLite的API不允许的 sqlite3_create_function
直接拨打电话通过Java。为了得到它的工作,你需要用的 SQLite的C库 =HTTP:/ /developer.android.com/sdk/ndk/index.html">NDK 。
Unfortunately, the SQLite API for Android does not allow for the sqlite3_create_function
call directly through Java. In order to get it to work you will need to compile the SQLite C library with the NDK.
如果你仍然有兴趣读 2.3用户定义的函数 ...
And if you are still interested read 2.3 User-defined functions...
下面是如何创建一个函数,查找字符串的第一个字符。
Here's how to create a function that finds the first character of a string.
static void firstchar(sqlite3_context *context, int argc, sqlite3_value **argv)
{
if (argc == 1) {
char *text = sqlite3_value_text(argv[0]);
if (text && text[0]) {
char result[2];
result[0] = text[0]; result[1] = '\0';
sqlite3_result_text(context, result, -1, SQLITE_TRANSIENT);
return;
}
}
sqlite3_result_null(context);
}
然后连接功能到数据库中。
Then attach the function to the database.
sqlite3_create_function(db, "firstchar", 1, SQLITE_UTF8, NULL, &firstchar, NULL, NULL)
最后,使用SQL语句的功能。
Finally, use the function in a sql statement.
SELECT firstchar(textfield) from table
这篇关于如何创建SQLite中一个用户定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!