Thank you Anthony for your quick response, it works fine.
I've been making some tests and have just found another way to access the 
table, just storing in a variable the result from define_table.

In the model:
*tb* = db.define_table(mytable, Field('myfield'))

and in the controller :
result = db().select(*tb*.myfield)


On Tuesday, 24 April 2012 14:55:22 UTC+2, Anthony wrote:
>
> Yes, DAL and Table both inherit from dict, so you can do:
>
> db[tablename][fieldname]
>
> to refer to a particular table and/or field. There are some examples of 
> this near the end of this section: 
> http://web2py.com/books/default/chapter/29/6#Record-representation.
>
> Anthony
>
> On Tuesday, April 24, 2012 7:28:51 AM UTC-4, Ralo Tannahill wrote:
>>
>> In the DB I am accessing the data is distributed into tables with the 
>> same structure but different names.
>> With the parameters received in the HTTP request I can obtain the table 
>> name I have to use (I have something like an "index" for this)
>> How can I use the table in web2py when its name is in a variable? I'm 
>> trying to avoid writing raw SQL queries.
>> I have found "with_alias" but I think I can't apply it here.
>>
>> For example, based on the parameters received I obtain that the query 
>> must be made to table named 'table123', and I set variable 
>> mytable='table123'.
>> I can define the model like this using the variable mytable:
>> db.define_table(mytable, Field('myfield'))
>>
>> I cannot use db().select(db.table123.ALL) because I dont' know the table 
>> name when I write the python code.
>> And I cannot use db().select(db.mytable.ALL) because mytable is the 
>> variable with the table name, not the name of the table.
>> Is there an alternate syntax to access the table whose name is in a 
>> variable?
>>
>> Thanks in advance, regards
>>
>>
>>
>>
>>
>>
>>

Reply via email to