How to find power of a number in SQLite

前端 未结 7 1844
悲&欢浪女
悲&欢浪女 2020-12-09 17:39

I want to update the Interest field in my database. My SQL query is like as per below

Update Table_Name set Interest = Principal * Power(( 1 + (rate

相关标签:
7条回答
  • 2020-12-09 18:30

    If you are using SQLite NuGet package in a .NET project, you can write an extension method and bind it at runtime;

    [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);
        }
    }
    

    And then use it like this;

    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();
    }
    
    0 讨论(0)
提交回复
热议问题