SOCI
Simple Oracle Call Interface (and more)

Connections and simple queries

Connecting to the database

The class 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.

Example:

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 Sessions 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.


Simple SQL statements


In many cases, the SQL query is intended to be executed only once, which means that statement parsing and execution can go together. The 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";
sql << "drop table " << tableName;

int id = 123;
sql << "delete from companies where id = " << id;

Previous (Errors)
Next (Exchanging data)


Copyright © 2004-2006 Maciej Sobczak, Stephen Hutton