Chapter 9. PyGreSQL - Python Interface

Table of Contents
9.1. The pg Module
9.2. pg Module Functions
connect -- opens a connection to the database server
get_defhost -- get default host name [DV]
set_defhost -- set default host name [DV]
get_defport -- get default port [DV]
set_defport -- set default port [DV]
get_defopt -- get default options specification [DV]
set_defopt -- set options specification [DV]
get_deftty -- get default connection debug terminal specification [DV]
set_deftty -- set default debug terminal specification [DV]
get_defbase -- get default database name specification [DV]
set_defbase -- set default database name specification [DV]
9.3. Connection object: pgobject
query -- executes a SQL command
reset -- resets the connection
close -- close the database connection
fileno -- returns the socket used to connect to the database
getnotify -- gets the last notify from the server
inserttable -- inserts a list into a table
putline -- writes a line to the server socket [DA]
getline -- gets a line from server socket [DA]
endcopy -- synchronizes client and server [DA]
locreate -- creates of large object in the database [LO]
getlo -- builds a large object from given oid [LO]
loimport -- imports a file to a PostgreSQL large object [LO]
9.4. Database wrapper class: DB
pkey -- returns the primary key of a table
get_databases -- get list of databases in the system
get_tables -- get list of tables in connected database
get_attnames -- returns the attribute names of a table
get -- get a tuple from a database table
insert -- insert a tuple into a database table
update -- update a database table
clear -- clear a database table
delete -- deletes the row from a table
9.5. Query result object: pgqueryobject
getresult -- gets the values returned by the query
dictresult -- like getresult but returns a list of dictionaries
listfields -- lists the fields names of the query result
fieldname -- field number-name conversion
fieldnum -- field name-number conversion
ntuples -- returns the number of tuples in query object
9.6. Large Object: pglarge
open -- opens a large object
close -- closes the large object
read -- reads from the large object
write -- writes to the large object
seek -- change current position in the large object
tell -- returns current position in the large object
unlink -- deletes the large object
size -- gives the large object size
export -- saves the large object to file
9.7. DB-API Interface

Author: Written by D'Arcy J.M. Cain (). Based heavily on code written by Pascal Andre . Copyright © 1995, Pascal Andre. Further modifications Copyright © 1997-2000 by D'Arcy J.M. Cain.

9.1. The pg Module

You may either choose to use the old mature interface provided by the pg module or otherwise the newer pgdb interface compliant with the DB-API 2.0 specification developed by the Python DB-SIG.

Here we describe only the older pg API. As long as PyGreSQL does not contain a description of the DB-API you should read about the API at http://www.python.org/topics/database/DatabaseAPI-2.0.html.

A tutorial-like introduction to the DB-API can be found at http://www2.linuxjournal.com/lj-issues/issue49/2605.html

The pg module defines three objects:

If you want to see a simple example of the use of some of these functions, see http://www.druid.net/rides where I have a link at the bottom to the actual Python code for the page.

9.1.1. Constants

Some constants are defined in the pg module dictionary. They are intended to be used as a parameters for methods calls. You should refer to the libpq description (Chapter 1) for more information about them. These constants are:

INV_READ
INV_WRITE

large objects access modes, used by (pgobject.)locreate and (pglarge.)open.

SEEK_SET
SEEK_CUR
SEEK_END

positional flags, used by (pglarge.)seek.

version
__version__

constants that give the current version