Chapter 3. Tutorial

Table of Contents

Before we begin programming
This document
C++ conventions
Setting up a connection
Performing a transaction
What's a Transaction?
Creating a transaction object
Ending a transaction
Executing queries
Getting query results
Doing Transactions Right: Transactors
Functors
How Transactors Work

Before we begin programming

This document

This frontend library is built on top of the libpq frontend library, which defines PostgreSQL™'s C API. Therefore this manual will sometimes refer to the libpq documentation rather than repeat what is said there. This was not done to annoy you, but to ensure that the libpqxx™ documentation remains up-to-date with any changes in the C frontend. Please bear with us.

Furthermore, if you're ever in doubt about how to use the basic features of libpqxx™, take a look at the example programs provided; they're called test000, test001 etc. Those should help give you an idea of how simple the library can be in practice. Don't let that keep you from submitting a request for improved documentation though; your questions define what this tutorial needs to say!

C++ conventions

All definitions made by libpqxx™ reside in a namespace pqxx. If you're not familiar with C++ namespaces, you have two options:

  • Always import the whole of the pqxx namespace in your programs.

    This tells the compiler to always look in the pqxx namespace when trying to resolve a name that doesn't occur in the global (ie. the "default") namespace). To do this, include the directive

    		using namespace pqxx;
    	      

    near the top of each source file that uses libpqxx™. This is typically best for your source files (you're probably doing it already for the std namespace), though it is not considered good practice in header files.

  • Explicitly qualify each libpqxx™ name with the pqxx namespace.

    This will tell the compiler exactly which names are supposed to be in the pqxx namespace, avoiding any confusion over multiple classes or functions used in your program possibly having the same name. As long as they're in different namespaces, the compiler will be able to tell them apart without trouble. To do this, write pqxx::connection instead of connection, pqxx::result::const_iterator instead of result::const_iterator, and so on.

    This is the mode of address you will typically want to use in header files, to avoid polluting the namespaces of the actual source files with the definitions of the extra namespace.