1.14.3.4 web.wsgi.database - Database Access

The web.wsgi.database module provides one class Database which adds the keys 'web.database.connection' and 'web.database.cursor' to the environ dictionary based on the parameters specified in the class constructor.

class Database( application, [**params])
application
A WSGI application or middleware component

**params
Any parameter supported by the web.database.connect() function

Entries added to environ:

environ['web.database.connection']
contains the connection object

environ['web.database.cursor']
contains the cursor object

Middleware or applications further down the chain can access the database through these objects as follows:

def myApp(environ, start_response):
    result = []
    result.append('<html>')
    self.environ['web.database.cursor'].execute('SELECT * FROM test')
    rows = self.environ['web.database.cursor'].fetchall()
    for row in rows:
        result.append('<p>%s</p>'%row)
    result.append('</html>')
    start_response('200 OK', [('Content-Type','text/html')])
    return result

application = web.wsgi.database.Database(
    myApp,
    type='MySQLdb',
    database='test',
)