Database Reference
In-Depth Information
INSERT INTO photos(image, thumbnail)
VALUES ($1,$2)
RETURNING id''', ('bytea', 'bytea'))
res = plpy.execute(q,
(raw_image_data,raw_thumbnail))
# return column id of first row
return res[0]['id']
$$ LANGUAGE plpythonu;
The Python code is more or less a straight rewrite from the PIL tutorial, except that
the files to read the image from and write the thumbnail image to, are replaced with
Python's standard file-like StringIO objects. For all this to work, you need to have
PIL installed on your database server host.
In Debian/Ubuntu, this can be done by running sudo apt.get install python-
imaging . On most modern Linux distributions, an alternative is to use Python's own
package distribution system by running sudo easy_install PIL .
Sending an e-mail
The next sample is a function for sending e-mails from inside a database function:
CREATE OR REPLACE FUNCTION send_email(
sender text, -- sender e-mail
recipients text, -- comma-separated list
of recipient addresses
subject text, -- email subject
message text, -- text of the message
smtp_server text -- SMTP server to use for
sending
) RETURNS void
AS $$
msg = "From: %s\r\nTo: %s\r\nSubject:
%s\r\n\r\n%s" % \
(sender, recipients, subject, message)
Search WWH ::




Custom Search