DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Adding A Function To SQLite In Python

  • submit to reddit
        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)


Snippets Manager replied on Mon, 2012/05/07 - 1:13pm

excelent stuff !