Android SQLite 加入自定义函数

SQLite Database 自定义函数实现:

//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);

//Finally, use the function in a sql statement.

//SELECT firstchar(textfield) from table

 

备注:aHR0cCUzQS8vd3d3LmNuYmxvZ3MuY29tL3poaGQv

参考代码:AOSP/external/sqlite/android/sqlite3_android.cpp

参考链接:http://stackoverflow.com/questions/7867099/how-can-i-create-a-user-defined-function-in-sqlite

参考链接:http://www.cnblogs.com/imouto/archive/2013/06/14/how-to-add-user-defined-functions-for-sqlite.html

posted @ 2016-09-23 10:10  seebigsea  阅读(950)  评论(0编辑  收藏  举报