Next topic

This Page

Quick search

A number of variables from SQLBuilder are included with fromsqlobjectimport* – see the relevant SQLObject documentation
for more. Its functionality is also available through the special
q attribute of SQLObject classes.

Most of the operators work properly: <, >, <=, >=, !=, ==, +, -, /,
*, **, %. However, and, or, and notdo not work.
You can use &, |, and ~ instead – but be aware that these have
the same precedence as multiplication. So:

# This isn't what you want:>>person.first_name=='John'&person.last_name=='Doe'(person.first_name=('John'ANDperson.last_name))='Doe')# This is:>>(person.first_name=='John')&(person.last_name=='Doe')((person.first_name='John')AND(person.last_name=='Doe'))

SQLBuilder also contains the functions AND, OR, and NOT which
also work – I find these easier to work with. AND and OR can
take any number of arguments.

You can also use .startswith() and .endswith() on an SQL
expression – these will translate to appropriate LIKE statements
and all % quoting is handled for you, so you can ignore that
implementation detail. There is also a LIKE function, where you
can pass your string, with % for the wildcard, as usual.

If you want to access an SQL function, use the func variable,
like:

>>person.created<func.NOW()

To pass a constant, use the const variable which is actually an
alias for func.

SQLBuilder implements objects that execute SQL statements. SQLObject
uses them internally in its higher-level API, but users can use this
mid-level API to execute SQL queries that are not supported by the
high-level API. To use these objects first construct an instance of a
statement object, then ask the connection to convert the instance to an
SQL query and finally ask the connection to execute the query and return
the results. For example, for Select class:

>>> fromsqlobject.sqlbuilderimport*>> select = Select(['name', 'AVG(salary)'], staticTables=['employees'],>> groupBy='name') # create an instance>> query = connection.sqlrepr(select) # Convert to SQL string:>> # SELECT name, AVG(salary) FROM employees GROUP BY name>> rows = connection.queryAll(query) # Execute the query>> # and get back the results as a list of rows>> # where every row is a sequence of length 2 (name and average salary)

A class to build SELECT queries. Accepts a number of parameters, all
parameters except items are optional. Use connection.queryAll(query)
to execute the query and get back the results as a list of rows.

items:

A string, an SQLExpression or a sequence of strings or
SQLExpression’s, represents the list of columns. If there are
q-values SQLExpression’s Select derives a list of tables for
SELECT query.

where:

A string or an SQLExpression, represents the WHERE clause.

groupBy:

A string or an SQLExpression, represents the GROUPBY clause.

having:

A string or an SQLExpression, represents the HAVING part of the
GROUPBY clause.

orderBy:

A string or an SQLExpression, represents the ORDERBY clause.

join:

A (list of) JOINs (LEFTJOIN, etc.)

distinct:

A bool flag to turn on DISTINCT query.

start, end:

Integers. The way to calculate OFFSET and LIMIT.

limit:

An integer. limit, if passed, overrides end.

reversed:

A bool flag to do ORDERBY in the reverse direction.

forUpdate:

A bool flag to turn on SELECTFORUPDATE query.

staticTables:

A sequence of strings or SQLExpression’s that name tables for
FROM. This parameter must be used if items is a list of strings
from which Select cannot derive the list of tables.

There are a few special operators that receive as parameter SQL
statements. These are IN, NOTIN, EXISTS, NOTEXISTS,
SOME, ANY and ALL. Consider the following example: You are
interested in removing records from a table using deleteMany. However,
the criterion for doing so depends on another table.