Chapter 11.3.1. sqlbuilder-Based Queries

SQLObject lets you have your cake and eat it, too. You can combine raw SQL power and easy object-oriented access with SQLObject. The key is the SQLBuilder module. This module defines a number of namespaces and classes that allow you to generate SQL expressions from objects. Suppose, for example, that you want to construct the following SQL query:

SELECT digit.name, digit.value

FROM digit

WHERE ((digit.value > 5) AND (digit.value < 9)):

from sqlobject.sqlbuilder import *

where_clause = AND(table.digit.value > 5, table.digit.value < 9)
columns = [Digit.q.name, Digit.q.value]
selectStatement = Select(columns, where_clause)
print selectStatement

To use the special sqlbuilder classes and operator, you must ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.