Over a million developers have joined DZone.

Adding A Function To SQLite In Python

·
Copy from David S's code here
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438802

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

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

...

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

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

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}