1.15.4.2 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.

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:

import web.wsgi.base, web.wsgi.cgi

class Application(web.wsgi.base.BaseApplication):
    def start(self):
        self.output('<html>')
        self.environ['web.database.cursor'].execute('SELECT * FROM test')
        results = self.environ['web.database.cursor'].fetchall()
        for result in results:
            self.output('<p>%s</p>'%result)
        self.output('</html>')

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