|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.
WHEREclause specified by where.
cursor.where(). If where is a string it is converted to the correct format.
cursor.order(). If order is a string it is converted to the correct format.
Falsethe method returns the SQL string needed to perform the desired operations. If
Truethe 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
Trueand execute is not specified execute is set to
Falsean error is raised.
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', )
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, record ... 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
The example above selected every
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.
WHEREclause suitable for use in the select(), update() and delete() methods of the
WHEREclause. Can include the
LIKEoperator which is used as follows:
WHERE columnName LIKE %s1%s2%s
Every % sign is matched against zero or more characters.
where should not include the string
'WHERE' at the beginning.
?parameters in the
More complex expressions can also be built into where clauses. See the SQL Reference section for full information.
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'
DESCafter 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
Alternatively we could have used the
cursor.order() method to help instead.
ORDER BYclause suitable for use in the select() method of the
ORDER BYclause. Note: order should not include the string
'ORDER BY'at the beginning.
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
>>> 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'))
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.
>>> rows = cursor.select( ... columns = ['table1.LastName', 'table2.Surname'], ... tables = ['table1','table2'], ... where = "table1.Surname = table2.Surname", ... format = 'dict', ... ) >>> print rows['table2.Surname'] 'Smith'