Subsections

1.4.13.1 Selecting Data

select( tables, columns, [values=[], ][where=None, ][order=None, ][execute=None, ][fetch=None, ][**params])

Build an SQL string according to the options specified and optionally execute the SQL and return the results in the format specified. No error checking on field names if the SQL string is only being built. Strict error checking is only performed when executing the code.

tables
A string containing the name of the table to select from or if selecting from multiple tables, a sequence of table names.
columns
A sequence of column names to select. Can be a string if only one column is being selected. If selecting from multiple tables, all column names should be in the form 'tableName.columnName'
values
A list of values to substitute for ? in the WHERE clause specified by where.
where
The WHERE clause as a web.database list as returned by cursor.where(). If where is a string it is converted to the correct format.
order
The ORDER BY clause as a web.database list as returned by cursor.order(). If order is a string it is converted to the correct format.
execute
If False the method returns the SQL string needed to perform the desired operations. If True the SQL is executed and the results converted and returned in the appropriate form. If not specified takes the value specified in the cursor which by default is True
fetch
Whether or not to fetch the results. If True and execute is not specified execute is set to True. If True and execute False an error is raised.
**params
The parameters to be passed to the fetchall() method if fetch is True

To select some information from a database using an SQL string you would use the following command:

SELECT column_name(s) FROM table_name

For example consider the table below:

# Table Person
+----------+-----------+---------+-------------+
| LastName | FirstName | Address | DateOfBirth |
+----------+-----------+---------+-------------+
| Smith    | John      | Bedford | 1980-01-01  |
+----------+-----------+---------+-------------+
| Doe      | John      | Oxford  | 1981-12-25  |
+----------+-----------+---------+-------------+

To retrieve a list of the surnames and dates of birth of all the people in the table you would use the following code:

rows = cursor.select(
    columns = ['LastName', 'DateOfBirth'], 
    tables  = ['Person'],
    format  = 'object',
)

Note: If you have specified fetch as False in the cursor constructor you would need to specify fetch as True here to fetch the results, otherwise you would need to use rows = cursor.fetchall() to actually fetch the results.

Since we have specified format as 'object', the result from this call would be a tuple of TupleDescriptor objects which can be treated as a tuple or a dictionary:

>>> for record in rows:
...    print record[0], record[1]
...    print record['LastName'], record['DateOfBirth']
...
Smith 1980-01-01
Smith 1980-01-01
Doe 1981-12-25
Doe 1981-12-25

Using the select() method, information you select from a field is automatically converted to the correct Python type. Integer fields return Integers, Date fields return datetime.date objects.

1.4.13.1.1 The where Parameter

The example above selected every LastName and DateOfBirth field from the table. To limit the information selected you need to specify the where parameter in the same way you would for any SQL query.

>>> rows=cursor.select(columns=['LastName'],tables=['Person'],where="LastName='Smith'")
>>> for record in rows:
...    print record['LastName'], record['DateOfBirth']
...
'Smith'

We had to specify the value Smith as properly encoded SQL since we specified the where clause as a string. Alternatively we could have used the cursor.where() method to help instead.

where( where, [values=[]])
Return a parsed WHERE clause suitable for use in the select(), update() and delete() methods of the cursor object.
where
A string containing the WHERE clause. Can include the LIKE operator which is used as follows:

WHERE columnName LIKE %s1%s2%s

Every % sign is matched against zero or more characters.

Note: where should not include the string 'WHERE' at the beginning.

values
A list of values to substitute for ? parameters in the WHERE clause

More complex expressions can also be built into where clauses. See the SQL Reference section for full information.

1.4.13.1.2 The order Parameter

You can specify the order in which the results are sorted using the order parameter. It is used as follows:

>>> for record in cursor.select('LastName', 'Person', order="'LastName'"):
...    print record['LastName']
...
'Doe'
'Smith'
>>> for record in cursor.select('LastName', 'Person', order="LastName DESC"):
...    print record['LastName']
...
'Smith'
'Doe'
Note that by placing the word DESC after the column to order by, the order is reversed.

You can place a number of Columns after each other. For example order="LastName DESC DateOfBirth" could be used to order the results in decending order by LastName and if any results have the same last name, order them by DateOfBirth.

Alternatively we could have used the cursor.order() method to help instead.

order( order)
Return a parsed ORDER BY clause suitable for use in the select() method of the cursor object.
order
A string containing the ORDER BY clause. Note: order should not include the string 'ORDER BY' at the beginning.

1.4.13.1.3 Disabling Execute

If you do not want the SQL to actually be executed you can set the execute parameter of the select() method to False. You can then manually execute it using cursor.execute().

>>> sql = cursor.select(columns=['LastName', 'DateOfBirth'], tables=['Person'], execute=False)
>>> sql
'SELECT LastName, DateOfBirth FROM Person'
>>> cursor.execute(sql)
>>> cursor.fetchall()
(('Smith','1980-01-01'),('Doe','1981-12-25'))

1.4.13.1.4 Using Joins

The select() allows you to select information from multiple tables. In order to do this you must specify the tables you wish to select from as a list or tuple and use the fully qualified column name for each table you want to column you want to select from.

For example:

>>> rows = cursor.select(
...     columns = ['table1.LastName', 'table2.Surname'],
...     tables  = ['table1','table2'], 
...     where   = "table1.Surname = table2.Surname",
...     format  = 'dict',
... )
>>> print rows[0]['table2.Surname']
'Smith'