Type Objects and Constructors
Many databases need to have the input in a
particular format in order to bind to an operation’s input
parameters. For example, if an input is destined for a
DATE column, it must be bound to the database in a
particular string format. Similar problems exist for “Row
ID” columns or large binary items (e.g.,
BLOBs or RAW columns). This
presents problems for Python since the parameters to the
executeXXX() method are untyped. When the database
module sees a Python string object, it doesn’t know if it
should be bound as a simple CHAR column, as a raw
BINARY item, or as a DATE.
To overcome this problem, a module must provide the constructors defined here to create objects that can hold special values. When passed to the cursor methods, the module can then detect the proper type of the input parameter and bind it accordingly.
A cursor object’s description attribute
returns information about each of the result columns of a query. The
type_code must compare equal to one of
type objects defined here. Type
objects may be equal to more than one type code (e.g.,
DATETIME could be equal to the type codes for
date, time, and timestamp columns; see the implementation hints later
in this appendix for details).
The module exports the following constructors and singletons:
-
Date(year,month,day) This function constructs an object holding a
datevalue.-
Time(hour,minute,second) This function constructs an object holding a
timevalue.-
Timestamp(year,month,day,hour,minute,second ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access