1.4.4.1 Understanding Field Types

Traditional SQL databases usually have support for a number of different fields. Date fields behave differently to integer fields for example. All of the fields are set using an SQL representation of the data in the form of a string and all of the queries from the database return strings.

The web.database module provides eight field types and rather than passing information to and from the database as specially formatted strings, you pass it as a python data structure. For example to set an Integer field you would give the cursor an integer. To set a Date field you would give the cursor a datetime.date object. The web.database cursor would do all the conversion for you.

Furthermore when you retrieve information from the database the cursor will convert the strings recieved back into Python objects so that you never need to worry about the encodings.

This doesn't sound like too much of a big deal but because different databases handle different datatypes in slightly different ways your SQL could have different results on different databases. Programming with a web.database cursor removes these inconsistencies.

There are currently eight field types supported and these are listed below.

Type  Description 
Char A character field taking strings of length 1
String A string field taking strings of up to 255 characters
Text A text field for storing large amounts of text (up to 16k characters)
Integer An integer field taking any integer that is a valid Python integer (but not long)
Float A float field taking Python float values
Date A date field. Takes values in the form of python datetime objects. Only stores days, months and years, any other information is trunkated. Dates from 0001-01-01 to 9999-12-31.
Time A time field. Takes values in the form of python datetime objects. Only stores hours, minutes and seconds, any other information is trunkated.
DateTime A datetime field. Takes values in the form of python datetime objects. Only stores days, months, years, hours, minutes and seconds, any other information is trunkated.

See About this document... for information on suggesting changes.