如何在SQLite中找到一个数字的权力 [英] How to find power of a number in SQLite
问题描述
我想更新我的数据库中的兴趣字段SQL查询如下所示
I want to update Interest field in my database SQL query is like as per below
更新Table_Name set Interest = Principal * Power (1 +(rate /
100),year)
Update Table_Name set Interest = Principal * Power(( 1 + (rate / 100),year)
此查询在MYSQL中正常工作, in SQLite for objetive c
This query is work fine in MYSQL but don't work in SQLite for objetive c
错误说没有电源功能
有没有人知道如何解决这个问题,因为我使用查询,因为我需要更新更多的3000条记录的时间。
Did anyone knows how to resolve this problem as I have do this using query because I need to update more thans 3000 record at time.
感谢提前
推荐答案
sqlite没有很多功能,但好消息是很容易添加自己的。
sqlite doesn't have a lot of functions available. But the good news is that is easy enough to add your own.
首先写一个幂函数:
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 *
数据库引用。
The 2
is the number of arguments for the function. dbRef
is of course the sqlite3 *
database reference.
这篇关于如何在SQLite中找到一个数字的权力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!