SQLite には、利用できる関数があまりありません。しかし、良いニュースは、独自のものを簡単に追加できることです。
C API (Objective-C コードからも機能します) を使用してそれを行う方法は次のとおりです。
最初に累乗関数を書きます:
void sqlite_power(sqlite3_context *context, int argc, sqlite3_value **argv) {
double num = sqlite3_value_double(argv[0]); // get the first arg to the function
double exp = sqlite3_value_double(argv[1]); // get the second arg
double res = pow(num, exp); // calculate the result
sqlite3_result_double(context, res); // save the result
}
次に、関数を登録する必要があります。
int res = sqlite3_create_function(dbRef, "POWER", 2, SQLITE_UTF8, NULL, &sqlite_power, NULL, NULL);
2
関数の引数の数です。dbRef
もちろん、sqlite3 *
データベース参照です。