Database Connections

The dbconn module defines DbConnection.

Database Connection

A DbConnection is a helper class representing a connection to a PostgreSQL database via the Psycopg adapter. It provides an easier interface than direct access to Psycopg. For example:

>>> from pyrseas.lib.dbconn import DbConnection
>>> db = DbConnection('dbname')
>>> db.fetchone("SHOW server_version")[0]
>>> db.commit()

A DbConnection is not necessarily connected. In the case of Pyrseas dbtoyaml and yamltodb, it will typically connect to the database when the DbObjectDict fetch() method is first invoked. It is normally disconnected just before the Database from_catalog() returns.

class pyrseas.lib.dbconn.DbConnection(dbname, user=None, pswd=None, host=None, port=None)

A database connection, possibly disconnected

DbConnection.connect()

Connect to the database

DbConnection.close()

Close the database connection

DbConnection.commit()

Commit currently open transaction

DbConnection.rollback()

Roll back currently open transaction

DbConnection.execute(query, args=None)

Create a cursor, execute a query and return the cursor

Parameters:
  • query – text of the statement to execute
  • args – arguments to query
Returns:

cursor

DbConnection.fetchone(query, args=None)

Execute a single row SELECT query and return row

Parameters:
  • query – a SELECT query to be executed
  • args – arguments to query
Returns:

a psycopg2 DictRow

The cursor is closed.

DbConnection.fetchall(query, args=None)

Execute a SELECT query and return rows

Parameters:
  • query – a SELECT query to be executed
  • args – arguments to query
Returns:

a list of psycopg2 DictRow’s

The cursor is closed.

Table Of Contents

Previous topic

Database Objects

Next topic

Databases

This Page