导入web2py的DAL以在App Engine上与Google Cloud SQL一起使用

时间:2012-03-19 07:21:35

标签: python google-app-engine web2py

我想在App Engine上构建一个应用程序,它使用Cloud SQL作为后端数据库而不是App引擎自己的数据存储工具(不支持常见的SQL操作,如JOIN)。

Cloud SQL有一个DB-API,因此我一直在寻找轻量级数据抽象层(DAL)来帮助轻松操作云数据库。一项小小的研究表明,web2py有一个非常整洁的DAL,它与Cloud SQL兼容。

由于我实际上并不需要整个全栈web2py框架,因此我将dal.py文件从/ gluon文件夹复制到一个简单的测试应用程序的主目录中,并将此行包含在我的应用程序中:

from dal import DAL, Field

db=DAL('google:sql://myproject:myinstance/mydatabase')

但是,在我部署应用程序并尝试运行它之后,这会产生错误。

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'

看起来这是由于声明

指定的'folder'属性出错
self.folder = folder or '$HOME/'+thread.folder.split('/applications/',1)[1]

是否有人知道此属性的作用以及如何解决此问题?

1 个答案:

答案 0 :(得分:0)

文件夹是DAL构造函数中的parm。它指向存储DB(sqlite)的文件夹。因此,我不认为这是您案件中的问题。我会再次检查连接字符串。

来自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.