Question

When i have the follow query:

str(db(db.items.id==int(row)).select(db.items.imageName)) + "\n"

The output includes the field name:

items.imageName  
homegear\homegear.jpg

How do i remove it so that field name will not be included and just the selected imagename.

i tried referencing it like a list [1] gives me an out of range error and [0] i end up with:

<Row {'imageName': 'homegear\\homegear.jpg'}>

The above is not a list, what object is that and how can i reference on it?

Thanks!

John

Was it helpful?

Solution

db(db.items.id==int(row)).select(db.items.imageName) returns a Rows object, and its __str__ method converts it to CSV output, which is what you are seeing.

A Rows object contains Row objects, and a Row object contains field values. To access an individual field value, you must first index the Rows object to extract the Row, and then get the individual field value as an attribute of the Row. So, in this case, it would be:

db(db.items.id==int(row)).select(db.items.imageName)[0].imageName

or:

db(db.items.id==int(row)).select(db.items.imageName).first().imageName

The advantage of rows.first() over rows[0] is that the former returns None in case there are no rows, whereas the latter will generate an exception (this doesn't help in the above case, because the subsequent attempt to access the .imageName attribute would raise an exception in either case if there were no rows).

Note, even when the select returns just a single row with a single field, you still have to explicitly extract the row and the field value as above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top