Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 1.41 KB

session.rst

File metadata and controls

38 lines (29 loc) · 1.41 KB

Session API

The Session class allows for a unified (and simplified) view of interfacing with a PostgreSQL database server.

Connection details are passed in as a PostgreSQL URI and connections are pooled by default, allowing for reuse of connections across modules in the Python runtime without having to pass around the object handle.

While you can still access the raw psycopg2 :py~psycopg2.extensions.connection and :py~psycopg2.extensions.cursor objects to provide ultimate flexibility in how you use the :pyqueries.Session object, there are convenience methods designed to simplify the interaction with PostgreSQL.

For psycopg2 functionality outside of what is exposed in Session, simply use the :pyqueries.Session.connection or :pyqueries.Session.cursor properties to gain access to either object just as you would in a program using psycopg2 directly.

Example Usage

The following example connects to the postgres database on localhost as the postgres user and then queries a table, iterating over the results:

import queries

with queries.Session('postgresql://postgres@localhost/postgres') as session:
    for row in session.query('SELECT * FROM table'):
        print row

Class Documentation

queries.Session