Database Reference
In-Depth Information
Strings in Python 2 are required to be in the PostgreSQL server encoding when they
are passed to PostgreSQL. Strings that are not valid in the current server encoding
will raise an error; but not all encoding mismatches can be detected, so garbage data
can still result when this is not done correctly. Unicode strings are converted to the
correct encoding automatically, so it can be safer and more convenient to use those.
In Python 3, all strings are Unicode strings.
In other words, anything but 0 , False, and an empty sequence, including empty
string ' ' or dictionary becomes PostgreSQL false .
One notable exception to this is that the check for None is done before any other
conversions and even for Booleans, None is always converted to NULL and not to
the Boolean value false .
For the bytea type, the PostgreSQL byte array, the conversion from Python's string
representation, is an exact copy with no encoding or other conversions applied.
Writing simple functions in PL/Python
Writing functions in PL/Python is not much different in principle from writing functions
in PL/pgSQL. You still have the exact same syntax around the function body in $$ ,
and the argument name, types, and returns all mean the same thing regardless of
the exact PL/language used.
A simple function
So a simple add_one() function in PL/Python looks like this:
CREATE FUNCTION add_one(i int)
RETURNS int AS $$
return i + 1;
$$ LANGUAGE plpythonu;
It can't get much simpler than that, can it?
Search WWH ::




Custom Search