1.7.1 Example

In order to use the web.session and web.auth modules the environment must be setup correctly. You can create the necessary environment using the web.environment module's driver() function as shown below:

#!/usr/bin/env python

# show python where the modules are
import sys; sys.path.append('../'); sys.path.append('../../../') 

# Setup a database connection
import web.database
connection = web.database.connect(
    adapter="snakesql", 
    database="environment",
    autoCreate = 1,
)
cursor = connection.cursor() 

import web.environment
driver = web.environment.driver(
    name='testEnv',
    storage='database',
    cursor=cursor,
)
if not driver.completeEnvironment():
    driver.removeEnvironment(ignoreErrors=True)
    driver.createEnvironment()
    print "Environment created"
else:
    print "Environment already complete"

connection.commit() # Save changes
connection.close()  # Close the connection

If none or only some of the tables are present we drop all the existing tables (ignoring errors produced because of missing tables) losing any information they contain and recreate all the tables. We also need to commit our changes to the database so that they are saved using connection.commit().