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.
| application, [**params]) |
Entries added to environ:
environ['web.database.connection']connection object
environ['web.database.cursor']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',
)