在python中使用sqlite的自定義函式功能

lt發表於2020-04-09

import sqlite3 as db
conn=db.connect("D:\fncov.db")
conn.create_function("sign", 1, lambda val: val and (val > 0 and 1 or -1))
cur = conn.cursor()
cur.execute("select sign(1), sign(-1), sign(0), sign(-99), sign(99)")

輸出

print (cur.fetchall())
[(1, -1, 0, -1, 1)]

import math
conn.create_function("log", 1, lambda val: math.log(val,10))
cur.execute("select log(2)")

輸出

print (cur.fetchall())
[(0.30102999566398114,)]

相關文章