Over a million developers have joined DZone.

Adding A Function To SQLite In Python

DZone's Guide to

Adding A Function To SQLite In Python

· ·
Free Resource
Copy from David S's code here

def _sign(val):
    if val:
        if val > 0: return 1
        else: return -1
        return val

#get your db connection, conn
conn.create_function("sign", 1, _sign)


>>cur = c.conn.cursor()
>>cur.execute("select test, val from test")
[(u'a', None)]

>>cur.execute("select sign(test), sign(val), sign(0), sign(-99), sign(99) from test")
[(1, None, 0, -1, 1)

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}