Never been to DZone Snippets before?

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

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Adding a function to SQLite in python

Copy from David S's code here
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438802
   1  
   2  def _sign(val):
   3      if val:
   4          if val > 0: return 1
   5          else: return -1
   6      else:
   7          return val
   8  
   9  #get your db connection, conn
  10  conn.create_function("sign", 1, _sign)
  11  
  12  ...
  13  
  14  >>cur = c.conn.cursor()
  15  >>cur.execute("select test, val from test")
  16  >>cur.fetchall()
  17  [(u'a', None)]
  18  
  19  >>cur.execute("select sign(test), sign(val), sign(0), sign(-99), sign(99) from test")
  20  >>cur.fetchall()
  21  [(1, None, 0, -1, 1)
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS