Import web2py's DAL to be used with Google Cloud SQL on App Engine
Question: I want to build an app on App Engine which uses Cloud SQL as backend database
instead of App engine's own datastore facility (which doesn't support common
SQL operations such as JOIN).

Cloud SQL has a DB-API and hence I was looking for a lightweight Data
Abstraction Layer (DAL) to help easily manipulate the cloud databases. A
little research revealed that web2py has a pretty neat DAL which is compatible
with Cloud SQL.

Since I don't actually need the whole full-stack web2py framework, I copied
the dal.py file out from the /gluon folder into a simple testing app's main
directory and included this line in my app:

    
    
    from dal import DAL, Field
    
    db=DAL('google:sql://myproject:myinstance/mydatabase')
    

However, this generated an error after I deployed the app and tried to run it.

    
    
    Traceback (most recent call last):
      File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__
        handler.get(*groups)
      File "/base/data/home/apps/jarod-helloworld/2.357593994022416181/helloworld2.py", line 13, in get
        db=DAL('google:sql://serangoon213home:rainman001/guestbook')
      File "/base/data/home/apps/jarod-helloworld/2.357593994022416181/dal.py", line 5969, in __init__
        raise RuntimeError, "Failure to connect, tried %d times:\n%s" % (attempts, tb)
    RuntimeError: Failure to connect, tried 5 times:
    Traceback (most recent call last):
      File "/base/data/home/apps/jarod-helloworld/2.357593994022416181/dal.py", line 5956, in __init__
        self._adapter = ADAPTERS[self._dbname](*args)
      File "/base/data/home/apps/jarod-helloworld/2.357593994022416181/dal.py", line 3310, in __init__
        self.folder = folder or '$HOME/'+thread.folder.split('/applications/',1)[1]
      File "/base/python_runtime/python_dist/lib/python2.5/_threading_local.py", line 199, in __getattribute__
        return object.__getattribute__(self, name)
    AttributeError: 'local' object has no attribute 'folder'
    

It looks like that it was due to an error with the 'folder' attribute which
was assigned by the statement

    
    
    self.folder = folder or '$HOME/'+thread.folder.split('/applications/',1)[1]
    

Does anyone know what this attribute does and how can I resolve this problem?


Answer: folder is a parm in the DAL contructor. It points to the folder where you
store DBs (sqlite). Thus, I don't think that's the problem in your case. I
would check again the connection string.

From the web2py docs:

    
    
    The DAL can be used from any Python program simply by doing this:
    
    from gluon import DAL, Field
    db = DAL('sqlite://storage.sqlite',folder='path/to/app/databases')
    i.e. import the DAL, Field, connect and specify the folder which contains the .table files (the app/databases folder).
    
    To access the data and its attributes we still have to define all the tables we are going to access with db.define_tables(...).
    
    If we just need access to the data but not to the web2py table attributes, we get away without re-defining the tables but simply asking web2py to read the necessary info from the metadata in the .table files:
    
    from gluon import DAL, Field
    db = DAL('sqlite://storage.sqlite',folder='path/to/app/databases',
             auto_import=True))
    This allows us to access any db.table without need to re-define it.
    

