문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top