Module functions and constants¶
The pgdb
module defines a connect()
function that allows to
connect to a database, some global constants describing the capabilities
of the module as well as several exception classes.
connect – Open a PostgreSQL connection¶
-
pgdb.
connect
([dsn][, user][, password][, host][, database])¶ Return a new connection to the database
Parameters: - dsn (str) – data source name as string
- user (str) – the database user name
- password (str) – the database password
- host (str) – the hostname of the database
- database – the name of the database
Returns: a connection object
Return type: Raises: pgdb.OperationalError – error connecting to the database
This function takes parameters specifying how to connect to a PostgreSQL
database and returns a pgdbCnx
object using these parameters.
If specified, the dsn parameter must be a string with the format
'host:base:user:passwd:opt:tty'
. All of the parts specified in the dsn
are optional. You can also specify the parameters individually using keyword
arguments, which always take precedence. The host can also contain a port
if specified in the format 'host:port'
. In the opt part of the dsn
you can pass command-line options to the server, the tty part is used to
send server debug output.
Example:
con = connect(dsn='myhost:mydb', user='guido', password='234$')
Module constants¶
-
pgdb.
apilevel
¶ The string constant
'2.0'
, stating that the module is DB-API 2.0 level compliant.
-
pgdb.
threadsafety
¶ The integer constant 1, stating that the module itself is thread-safe, but the connections are not thread-safe, and therefore must be protected with a lock if you want to use them from different threads.
-
pgdb.
paramstyle
¶ The string constant
pyformat
, stating that parameters should be passed using Python extended format codes, e.g." ... WHERE name=%(name)s"
.
Errors raised by this module¶
The errors that can be raised by the pgdb
module are the following:
-
exception
pgdb.
Warning
¶ Exception raised for important warnings like data truncations while inserting.
-
exception
pgdb.
Error
¶ Exception that is the base class of all other error exceptions. You can use this to catch all errors with one single except statement. Warnings are not considered errors and thus do not use this class as base.
-
exception
pgdb.
InterfaceError
¶ Exception raised for errors that are related to the database interface rather than the database itself.
-
exception
pgdb.
DatabaseError
¶ Exception raised for errors that are related to the database.
In PyGreSQL, this also has a
DatabaseError.sqlstate
attribute that contains theSQLSTATE
error code of this error.
-
exception
pgdb.
DataError
¶ Exception raised for errors that are due to problems with the processed data like division by zero or numeric value out of range.
-
exception
pgdb.
OperationalError
¶ Exception raised for errors that are related to the database’s operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, or a memory allocation error occurred during processing.
-
exception
pgdb.
IntegrityError
¶ Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails.
-
exception
pgdb.
ProgrammingError
¶ Exception raised for programming errors, e.g. table not found or already exists, syntax error in the SQL statement or wrong number of parameters specified.
-
exception
pgdb.
NotSupportedError
¶ Exception raised in case a method or database API was used which is not supported by the database.