How to select row based on item ID or name

47 views
Skip to first unread message

Maurice Waka

unread,
Jan 4, 2018, 1:48:32 AM1/4/18
to web2py-users






I have the following code for my DB:
dbmyhealth.define_table("health",
                            Field('name', 'string'),
                            Field('definition', 'text', length= 1000000,),
                            Field('abnval', 'text', length= 1000000,),
                            Field('normval', 'text', length= 1000000,),
                            Field('administration', 'text', length= 1000000,),
                            Field('screening', 'text', length= 1000000,),
                            Field('causes', 'text', length= 1000000,),
                            migrate = False)

rows  = dbmyhealth().select(dbmyhealth.health.ALL)
for row in rows:
    location0 = row.name
    location1 = row.definition
    location2 = row.abnvalinterpret
    location3 = row.normvalinterpret
    location4 = row.administration
    location5 = row.screening
    location6 = row.causes
    corpus = [location1 , location2, location3, location4, location5, location6]

I want to select an item, including all the fields from the abnval to the causes,not randomly but based on the ID or the name. If I use the limitby constraint,I can get either the first, second, or last item based on the selection used. If I use the below code, I get only the first item, which is everything based on obesity. I want to select any item with every query using either an ID or the names below e.g.
A code like: 
for row in rows:
    if id == 2
        code..
           corpus = [location1 , location2, location3, location4, location5, location6]....in this case my corpus will have everything on cardiomyopathy using the table details below.            

id name
0. diabetes
1. hypertension
2. cardiomyopathy
3. copd
4. obesity

So how do I code it to obtain my answer
Kind regards


Dave S

unread,
Jan 4, 2018, 12:24:26 PM1/4/18
to web2py-users
Wouldn't that be either of

      rows = dbmyhealth(dbmyhealth.health.id == 2).select()



or (if you were doing more with the query)

      query = dbmyhealth.health.id == 2
      rows  
= dbmyhealth(query).select()


?
(I didn't put in a first() because there's only 1 row with id == 2.)


/dps

Maurice Waka

unread,
Jan 4, 2018, 1:13:45 PM1/4/18
to web...@googlegroups.com
Second option is better for me 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/5DWqynKI_-g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yoel Benítez Fonseca

unread,
Jan 4, 2018, 2:27:48 PM1/4/18
to web...@googlegroups.com
You can get algo a row by ID by:

row = db.model_name(ID)

Where ID stand for the actual record id.
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Paul Ellis

unread,
Jan 4, 2018, 4:42:54 PM1/4/18
to web2py-users
You would still get a Rows Object though, even though there is only 1 row in it.
You would need .first() .last() or [0] at the end to get just the row.
Or use the shortcut method.

db.table[id]
Reply all
Reply to author
Forward
0 new messages