Session
encapsulates the database connection
and other backend-related details, which are common to all the
statements
that will be later executed. Its constructor expects two parameters:
the requested backend name and the generic connection string, which
meaning is backend-dependent.Session sql("oracle",
"service=orcl user=scott password=tiger"); |
Another example might be:
Session sql("postgresql",
"dbname=mydb"); |
Above, the sql
object is a local (automatic) object
that encapsulates the connection.
The Session
constructor either connects successfully, or
throws the
exception.
It is possible to have many active Session
s at the same
time, even using different backends.
Portability note:
The "oracle"
and "postgresql"
are currently
the only backends available. The name is case-sensitive.
The set of parameters used in the connection string for Oracle is:
All of these parameters have to be provided.
The set of parameters available for PostgreSQL is the same as that
recognized by the PQconnectdb
function and may depend on
the server version. See PostgreSQL documentation for details.
Session
class provides a special once
member, which triggers parsing and execution of such one-time
statements:
sql.once << "drop table persons"; |
For shorter syntax, the following form is also allowed:
sql << "drop table persons"; |
The IOStream-like interface is exactly what it looks like, so that
the
statement text can be composed of many parts, involving anything that
is streamable (including custom classes, if they have
appropriate operator<<
):
string tableName = "persons"; |
Previous
(Errors) |
Next (Exchanging data) |