You can do this:
query="%s.id>0" % (table) records=SQLTABLE(db(query).select()) or this: query=db[table].id>0 records=SQLTABLE(db(query).select()) Notice that in the first case you have quotes so you are writing raw SQL, hence no db exists (the db connection object is only known to web2py not the database). I suggest using the second syntax since the former may be vulnerable to SQL injections. Massimo On Apr 15, 4:17 am, Samit Pal <sami...@gmail.com> wrote: > Hi, > > I am a python and web2py newbie. Sorry in advance for the dumb question. > > I am trying to use the following code > > table=request.vars.table # This gives us the table name to query > query="db.%s.id>0" % (table) # this sets up the query > > records=SQLTABLE(db(query).select()) # this should get us records. > > But i get the folloeing error. > > File > "/usr/local/google/home/samit/web2py/applications/myapp/controllers/mytestcontroller.py", > line 27, in tracked_in_logs > records=SQLTABLE(db(query).select()) > File "/usr/local/google/home/samit/web2py/gluon/sql.py", line 1901, in > select > query = self._select(*fields, **attributes) > File "/usr/local/google/home/samit/web2py/gluon/sql.py", line 1812, in > _select > fields = [self._db[table].ALL for table in self._tables] > File "/usr/local/google/home/samit/web2py/gluon/sql.py", line 435, in > __getitem__ > return dict.__getitem__(self, str(key)) > KeyError: 'db' > > Is it a problem with getting the value of the variable 'query' not getting > expanded --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py Web Framework" group. To post to this group, send email to web2py@googlegroups.com To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---