2012-03-19 85 views
1

我想在App Engine上構建應用程序,該應用程序使用Cloud SQL作爲後端數據庫而不是App引擎自己的數據存儲設施(不支持常見的SQL操作,如加入)。導入web2py的DAL用於App Engine上的Google Cloud SQL

雲SQL有一個DB-API,因此我一直在尋找一個輕量級的數據抽象層(DAL)來幫助輕鬆操縱雲數據庫。一個小小的研究表明,web2py有一個與Cloud SQL兼容的漂亮DAL。

因爲我實際上並不需要整個全棧的web2py框架,我複製了dal.py從/膠子文件夾中的文件進行到一個簡單的測試應用程序的主目錄,其中包括此行中我的應用程序:

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] 

有誰知道這個屬性做什麼,我怎麼能解決這個問題?

回答

0

文件夾是DAL構造器中的一個parm。它指向你存儲數據庫(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. 
相關問題