[code.view]

[top] / python / PyMOTW / sqlite3 / sqlite3_create_function.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Creating functions to run in SQL statements.
     """
     #end_pymotw_header
     
     import sqlite3
     
     db_filename = 'todo.db'
     
     def encrypt(s):
         print 'Encrypting %r' % s
         return s.encode('rot-13')
     
     def decrypt(s):
         print 'Decrypting %r' % s
         return s.encode('rot-13')
     
     
     with sqlite3.connect(db_filename) as conn:
     
         conn.create_function('encrypt', 1, encrypt)
         conn.create_function('decrypt', 1, decrypt)
         cursor = conn.cursor()
     
         # Raw values
         print 'Original values:'
         query = "select id, details from task"
         cursor.execute(query)
         for row in cursor.fetchall():
             print row
     
         print '\nEncrypting...'
         query = "update task set details = encrypt(details)"
         cursor.execute(query)
         
         print '\nRaw encrypted values:'
         query = "select id, details from task"
         cursor.execute(query)
         for row in cursor.fetchall():
             print row
         
         print '\nDecrypting in query...'
         query = "select id, decrypt(details) from task"
         cursor.execute(query)
         for row in cursor.fetchall():
             print row
     

[top] / python / PyMOTW / sqlite3 / sqlite3_create_function.py

contact | logmethods.com