How to find power of a number in SQLite

前端 未结 7 1954
悲&欢浪女
悲&欢浪女 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条回答
  •  旧时难觅i
    2020-12-09 18:09

    You can also create an SQLite user-defined function from python. Based on the example at docs.python.org: sqlite3.Connection.create_function

    Create a python function:

    def sqlite_power(x,n):
        return int(x)**n
    print(sqlite_power(2,3))
    # 8    
    

    Create a SQLite user-defined function based on the python function:

    con = sqlite3.connect(":memory:")
    con.create_function("power", 2, sqlite_power)
    

    Use it:

    cur = con.cursor()
    cur.execute("select power(?,?)", (2,3))
    print cur.fetchone()[0]
    # 8
    

提交回复
热议问题