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

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

Comments

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

excelent stuff !