On Apr 26, 4:38 pm, frankz <[email protected]> wrote:
> Hi,
> in the web2py official book, in the image blog example,

Referring to http://web2py.com/book/default/section/3/6?search=image+blog

> Could anyone help explain what the following line mean?
> image = db(db.image.id==request.args(0)).select()[0]

This example code assume's you've entered an image in the database; if
you have not,
then the   ....select()[0] reference would give you index out of
range.

Here's why:

- This is a database select query;  it's form is basically:

your_db_connection( WHERE clause; in this case where ID of the image
table is same as what requested).select( fields - "ALL" by default)

This returns an array of records found in the database - if none, it
returns an empty array, [].

A more robust way to write this (not used, so the example would be
short) would be something like this:

images = db(db.image.id==request.args(0)).select()
if len(images) == 0:  print "No images found!"
else:  image = images[0]

I hope this has been helpful.
Regards,
- Yarko


>
> I duplicate the same code but I find URL
> htttp://127.0.0.1:8000/images/default/show
> gives me an error, "index out of range"
>
> How do I fix it?
>
> Thanks,
>
> --
> Subscription settings:http://groups.google.com/group/web2py/subscribe?hl=en

Reply via email to