Query
, Set
, Rows
Let’s consider again the table defined (and dropped) previously and insert three records:
>>> db.define_table('person', Field('name'))
<Table person (id, name)>
>>> db.person.insert(name="Alex")
1
>>> db.person.insert(name="Bob")
2
>>> db.person.insert(name="Carl")
3
You can store the table in a variable. For example, with variable person
, you could do:
>>> person = db.person
You can also store a field in a variable such as name
. For example, you could also do:
>>> name = person.name
You can even build a query (using operators like ==, !=, <, >, <=, >=, like, belongs) and store the query in a variable q
such as in:
>>> q = name == 'Alex'
When you call db
with a query, you define a set of records. You can store it in a variable s
and write:
>>> s = db(q)
Notice that no database query has been performed so far. DAL + Query simply define a set of records in this db that match the query. web2py determines from the query which table (or tables) are involved and, in fact, there is no need to specify that.