Database 如何在SQLite中找到数字的幂
我想更新数据库中的兴趣字段。我的SQL查询如下所示 更新表_Name set Interest=本金*Power((1+)(利率/ 100),年) 此查询在MySQL中运行良好,但不适用于SQLite 错误表示未找到幂函数Database 如何在SQLite中找到数字的幂,database,sqlite,Database,Sqlite,我想更新数据库中的兴趣字段。我的SQL查询如下所示 更新表_Name set Interest=本金*Power((1+)(利率/ 100),年) 此查询在MySQL中运行良好,但不适用于SQLite 错误表示未找到幂函数 有没有人知道如何解决这个问题,因为我必须使用query一次更新3000多条记录。SQLite不提供power函数或操作符。您必须自己通过来实现。SQLite没有很多可用的函数。但好消息是,添加自己的函数很容易 下面介绍如何使用C API(它也可以从Objective-C代码中
有没有人知道如何解决这个问题,因为我必须使用query一次更新3000多条记录。SQLite不提供power函数或操作符。您必须自己通过来实现。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);
def sqlite_power(x,n):
return int(x)**n
print(sqlite_power(2,3))
# 8
con = sqlite3.connect(":memory:")
con.create_function("power", 2, sqlite_power)
然后需要注册函数:
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);
def sqlite_power(x,n):
return int(x)**n
print(sqlite_power(2,3))
# 8
con = sqlite3.connect(":memory:")
con.create_function("power", 2, sqlite_power)
2
是函数的参数数量。dbRef
当然是sqlite3*
数据库参考。
第二步是建立数学扩展库,一位名叫利亚姆·希利的杰出人士写道:
在终端中输入以下命令:
步骤1)下载/打开链接
步骤2)转到下载扩展函数.c的位置。运行命令“gcc-fno common-dynamiclib extension functions.c-o libsqlitefunctions.dylib”。这将在同一位置创建文件libsqlitefunctions.dylib,然后您可以从xcode在ios应用程序中使用该文件
现在,您可以在cocoa应用程序中添加:
“SELECT load_extension(’libsqlitefunctions.dylib’);”
然后你就可以使用各种优秀的方法,比如COS、SQRT等等!您可以在应用程序中使用它们,如下所示:
//Activate database loading
sqlite3_enable_load_extension(database, 1);
sqlite3_load_extension(database,”libsqlitefunctions.dylib”,0,0);
如果您在.NET项目中使用SQLite NuGet包,则可以编写扩展方法并在运行时绑定它
[SQLiteFunction("pow", 2, FunctionType.Scalar)]
public class SQLitePowerExtension : SQLiteFunction
{
public override object Invoke(object[] args)
{
double num = (double)args[0];
double exp = (double)args[1];
return Math.Pow(num, exp);
}
}
然后像这样使用它
using (var conn = new SQLiteConnection("Data Source=:memory:"))
{
conn.Open();
conn.BindFunction(typeof(SQLitePowerExtension).GetCustomAttribute<SQLiteFunctionAttribute>(), new SQLitePowerExtension());
var comm = new SQLiteCommand("CREATE TABLE test (num REAL, exp REAL, result REAL)", conn);
comm.ExecuteNonQuery();
// Populate with some data - not shown
comm = new SQLiteCommand($"UPDATE test SET result = pow(num, exp))", conn);
comm.ExecuteNonQuery();
}
使用(var conn=new SQLiteConnection(“数据源=:内存:”)
{
conn.Open();
conn.BindFunction(typeof(SQLitePowerExtension).GetCustomAttribute(),新的SQLitePowerExtension());
var comm=new SQLiteCommand(“创建表测试(num REAL、exp REAL、result REAL)”,conn;
comm.ExecuteNonQuery();
//填充一些数据-未显示
comm=newsqlitecommand($“更新测试集结果=pow(num,exp))”,conn);
comm.ExecuteNonQuery();
}
您还可以从python创建一个SQLite用户定义函数。根据docs.python.org上的示例:
创建python函数:
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);
def sqlite_power(x,n):
return int(x)**n
print(sqlite_power(2,3))
# 8
con = sqlite3.connect(":memory:")
con.create_function("power", 2, sqlite_power)
基于python函数创建SQLite用户定义函数:
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);
def sqlite_power(x,n):
return int(x)**n
print(sqlite_power(2,3))
# 8
con = sqlite3.connect(":memory:")
con.create_function("power", 2, sqlite_power)
使用它:
cur = con.cursor()
cur.execute("select power(?,?)", (2,3))
print cur.fetchone()[0]
# 8
这可以用SQL来解决。这适用于整数指数:
Drop Table if Exists args ;
Create Table args as Select 2.5 as Base, 4 as Exponent ;
WITH RECURSIVE pow(exponent, exponent_remainder, base, result) as (
--FIRST EXPRESSION
SELECT exponent,exponent -1 , base,base
FROM args
union all
--SECOND EXPRESSION
select Args.exponent,pow.exponent_remainder -1, pow.base,pow.result * pow.base
from args
join pow on args.exponent = pow.exponent
where pow.exponent_remainder >= 0
)
select pow.result
from pow
where pow.exponent_remainder = 0;
我也一直在讨论这个问题,但如果你只需要2的幂(或倍数,等等),有一个更简单的方法: 使用换档操纵器,例如
SELECT 1 << mytable.value
SELECT 1 << (table.x + etc..)
SELECT 1实际上,sqlite确实有pow/power
作为一个内置的数学函数,但您需要使用DSQLITE\u enable\u MATH\u函数启用它
发件人:
下面显示的数学函数是SQLite合并源文件的一部分,但只有在使用-DSQLITE_ENABLE_math_functions compile-time选项编译合并时才处于活动状态
谢谢你!为了简单起见,为了简洁起见,我只做了一行:con.create_函数(“power”,2,lambda x,y:x**y)
。实际上Sqlite确实有。但您必须使用一个特殊的标志来编译才能启用它。这个问题最初是在2012年提出的。当时,没有电源功能。数学函数直到2021-03-12(版本3.35.0)才添加,这是问题提出后的8年。