【问题标题】:Import web2py's DAL to be used with Google Cloud SQL on App Engine导入 web2py 的 DAL 以与 App Engine 上的 Google Cloud SQL 一起使用
【发布时间】:2012-04-03 17:09:06
【问题描述】:

我想在 App Engine 上构建一个应用,它使用 Cloud SQL 作为后端数据库,而不是 App Engine 自己的数据存储设施(不支持常见的 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'

看起来这是由于语句分配的“文件夹”属性错误

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

有谁知道这个属性有什么作用,我该如何解决这个问题?

【问题讨论】:

    标签: python google-app-engine web2py


    【解决方案1】:

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

    来自 web2py 文档:

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-05
      • 2014-04-10
      • 2018-11-05
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      相关资源
      最近更新 更多