2012-08-14 43 views
2

我試圖運行在使用該模型用下面的命令的命令行腳本:的web2py DB沒有被定義

c:\web2py>python web2py.py -M -N -S automate -R applications/automate/modules/eventserver.py 

,但我不斷收到錯誤:

web2py Web Framework 
Created by Massimo Di Pierro, Copyright 2007-2011 
Version 1.99.7 (2012-03-04 22:12:08) stable 
Database drivers available: SQLite3, pymysql, pg8000, IMAP 
Traceback (most recent call last): 
    File "c:\web2py\gluon\shell.py", line 206, in run 
    execfile(startfile, _env) 
    File "applications/automate/modules/eventserver.py", line 6, in <module> 
    deviceHandler = devicehandler.DeviceHandler() 
    File "applications\automate\modules\devicehandler.py", line 10, in __init__ 
    self.devices = self.getActiveDevices() 
File "applications\automate\modules\devicehandler.py", line 18, in getActiveDe 
vices 
    print db 
NameError: global name 'db' is not defined 

什麼我做錯了嗎?

編輯:從我的研究,我只找到了解決辦法「-M添加到您的命令」,但我已經這樣做了,它仍然不起作用。

EDIT2:我已經DB = DAL:在我db.py( 'sqlite的//storage.sqlite'),所以它應該得到加載

+2

*「全局名稱‘DB’沒有定義」 * – 2012-08-14 20:09:59

+0

爲什麼我收到downrated?我究竟做錯了什麼? :( – Henrock 2012-08-14 21:07:42

回答

7

edit2: I have db = DAL('sqlite://storage.sqlite') in my db.py so it should get loaded

假設db.py是在/模型文件夾,在那裏創建的db對象將在稍後執行的模型文件以及控制器和視圖中可用,但在導入的模塊中將不可用。相反,您必須將db對象傳遞給模塊中的函數或類。另一種選擇是將db對象添加到current線程本地對象,然後可將其導入和訪問的模塊內:

在/models/db.py:

from gluon import current 
db = DAL('sqlite://storage.sqlite') 
current.db = db 

在/模塊/ EVENTSERVER的.py:

from gluon import current 
def somefunction(): 
    db = current.db 
    [do something with db] 

注意,如果你定義模塊中的db對象,在頂層沒有定義它 - 在一個函數或類定義它。

有關詳細信息,請參閱modules and current書節。

+0

我明白了。我會在我下班了很多嘗試。感謝您的幫助!:) – Henrock 2012-08-15 06:39:18

+0

嗨,我嘗試了上述解決方案,但我得到的錯誤'<類的psycopg2.InterfaceError'>光標已經closed'有任何想法嗎? – 2013-04-10 19:15:01

+0

你能表現出一定的代碼(也許打開一個新的問題,在這裏或在[谷歌集團(https://groups.google.com/forum/#!forum/web2py))? – Anthony 2013-04-10 23:33:43