python - list Index error -


  • updated - hi have "half" problem web development:

i have method in class takes value database call, (the table in database has key: catid , field)

def getcat(self):         ''' devuelve el numero de la categoria         '''         = self.datab.select('categorias',where='catname = $nombre', vars=dict(nombre=self.nombre))         a= a.list()         return a[0].catid      context: use make categorization in web content, want return value wich join content table in database. 

so if give self.nombre wich string stored in database row i'll ever take 1 element database.

the python interpreter gives me error:

  file "c:\xampp\htdocs\webpy\functions.py", line 26, in getcat     return a[0].catid indexerror: list index out of range 

127.0.0.1:51463 - - [05/apr/2013 13:29:22] "http/1.1 /c/\css\style.css" - 500 internal server error

but web gives out information correctly, have no problem web output wish know why error in indexing a[0]

i made a.list() in order simplify uses of itterbetter

complete error mesage in idle interpreter:

0.0 (1): select catname categorias order catid asc  127.0.0.1:51900 - - [05/apr/2013 16:46:31] "http/1.1 /c/" - 200 ok 0.0 (1): select * categorias catname = '\\css\\style.css' traceback (most recent call last):   file "c:\python27\lib\site-packages\web\application.py", line 236, in process     return self.handle()   file "c:\python27\lib\site-packages\web\application.py", line 227, in handle     return self._delegate(fn, self.fvars, args)   file "c:\python27\lib\site-packages\web\application.py", line 409, in _delegate     return handle_class(cls)   file "c:\python27\lib\site-packages\web\application.py", line 384, in handle_class     return tocall(*args)   file "c:\xampp\htdocs\webpy\code.py", line 32, in     seleccion = functions.categoria(db,cat)   file "c:\xampp\htdocs\webpy\functions.py", line 16, in __init__     self.n = self.getcat() #calculo del numero de la categoria problemas   file "c:\xampp\htdocs\webpy\functions.py", line 26, in getcat     return a[0].catid indexerror: list index out of range 

you need proper error handling, can either do:

try:     a[0].catid except indexerror ie:     # stuff here except:     # either re-raise exception or pass silently 

or can do:

return , a[0].catid or [] # check empty list 

it depends on implementation choice.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -