On Sun, Mar 10, 2013 at 9:48 PM, Kenan <
ken...@gmail.com> wrote:
> Interesting question, I have a similar problem. Really, why can't we select
> only one field with select() ?
>
> For example, If i have several field in table Detailtip2 and want to select
> only top 10 amounts:
> rows = db((db.Detailtip2.year==session.year) &
> (db.Detailtip2.budget==session.budget)).select(db.Detailtip2.amount,
> orderby=~db.Detailtip2.amount, limitby=(0, 9))
>
> The problem is that rows gets populated with all fields from table.Usually,
> it's not a problem, but in this case i want to populate a list for bar
> chart:
> chart_data_list = rows.as_list()
>
> It doesn't work as expected since I got all fields and can't draw a chart.
> OK, I have another solution for this:
> for row in rows:
> chart_data_list.append(row.amount/)
>
> But I still have a question: How can I select one specific field?
I did a quick test and this works for me with sqlite in web2py shell:
>>> db.define_table('person', Field('first_name'), Field('last_name'))
<Table person (id,first_name,last_name)>
>>> db(db.person)._select(
db.person.id)
'SELECT
person.id FROM person WHERE (
person.id IS NOT NULL);'
>>> db(db.person)._select(
db.person.id, db.person.last_name)
'SELECT
person.id, person.last_name FROM person WHERE (
person.id IS NOT NULL);'
>>> db()._select(db.person.ALL)
'SELECT
person.id, person.first_name, person.last_name FROM person;'
>>> db(db.person)._select()
'SELECT
person.id, person.first_name, person.last_name FROM person
WHERE (
person.id IS NOT NULL);'
Now, for web2py devs, are those below supported, I don't remember see anything
about this in the book.
>>> db(db.person)._select('*')
'SELECT * FROM person WHERE (
person.id IS NOT NULL);'
>>> db(db.person)._select('id')
'SELECT id FROM person WHERE (
person.id IS NOT NULL);'
>>> db(db.person)._select('id,last_name')
'SELECT id,last_name FROM person WHERE (
person.id IS NOT NULL);'
Ricardo