OCILIB Tutorials v5.0.0

OCILIB Tutorials

OCILIB is a free, open-source C and C++ driver for Oracle Database.

It is proven, mature technology — actively maintained for nearly 20 years and distributed under the Apache License 2.0.

OCILIB wraps the low-level Oracle Call Interface (OCI) — the same C library that Oracle's own drivers use — in a clean, object-oriented API, while still exposing the full power of Oracle.

These tutorials take you from "I just heard of OCILIB" to "I can use every part of the API", step by step.

Why OCILIB?

Raw OCI is powerful but famously painful: dozens of opaque handle types, attribute getters/setters that take void*, manual descriptor management, and pages of boilerplate for a simple SELECT.

OCILIB keeps the power and removes the pain:

How OCILIB is designed

Understanding the shape of the library makes every tutorial page obvious:

 your code
     │
 ┌───▼───────────────────────────┐
 │ C++ API  (header-only, C++17) │   optional layer — RAII wrappers,
 │   ocilib::Connection, …       │   exceptions, iterators
 ├───▼───────────────────────────┤
 │ C API    (ISO C11)            │   OCI_<Type> handles, OCI_<Type><Action>()
 │   OCI_Connection, …           │   error handler / OCI_GetLastError
 ├───▼───────────────────────────┤
 │ OCI      (Oracle client)      │   shipped by Oracle — Instant Client
 │   OCIServerAttach, …          │   or full client, linkage or runtime import
 └───▼───────────────────────────┘
 Oracle Database

Three design decisions explain everything else you will learn:

  1. The environment is the root object. OCI_EnvironmentCreate() loads the Oracle client (it can even locate oci.dll/libclntsh.so at run time), installs your error handler, and owns every other handle. Create it once, free it last.
  2. Handles form an ownership tree. A Connection owns its Statements; a Statement owns its Resultsets and Binds. Freeing a parent frees its children — and in C++, destructors do it all for you in reverse order.
  3. Errors are data, not exceptions — unless you want them to be. In C, failures invoke your error callback with an OCI_Error (type, category, code, message) and are also available through OCI_GetLastError(). In C++, the same errors become ocilib::Exception thrown automatically.

How the tutorials work

Every page is a self-contained lesson: objectives → concepts → a guided walkthrough → a complete code example → short exercises → pitfalls → recap. Code is always shown in both C and C++ — click the tabs on any code card:

OCI_Connection *cn = OCI_ConnectionCreate(env, "localhost/XEPDB1", "scott", "tiger",
                                        OCI_SESSION_DEFAULT);

Your choice is remembered across all pages.

Conventions used everywhere

The learning path

Part What you will learn
0 — Getting started Install Oracle client + OCILIB on Windows/Linux, MBCS vs wide charset builds, first program, first connection, error handling, diagnostics logging
1 — Connections & environments Connection strings, session modes, ping/break, introspection
2 — Oracle data types Charsets, dates, timestamps, intervals, numbers, RAW, vectors, LOBs, BFILEs, XML, JSON, PL/SQL BOOLEAN & RECORD, user-defined types
3 — SQL, fetching & binding Statements, resultsets, every getter, bind variables, scrollable cursors, bulk fetch
4 — PL/SQL Blocks, OUT parameters, ref cursors, index-by tables, DBMS_OUTPUT
5 — Transactions, pooling & threads Commit/rollback, transaction objects, connection/session pools, multithreading
6 — Object-relational API Type describe, objects, collections, REFs
7 — Messaging & notifications Advanced Queuing, publish/subscribe, change notification (CQN)
8 — Bulk & administration Array binding, direct path loading, database startup/shutdown
9 — Modern Oracle features Sharding, SODA document store
Appendices C ↔ C++ API map, type cheat sheets, build flags, FAQ, demo index

Ready? Continue to Your first connection — or jump to any topic in the sidebar.

Prerequisites

You need an Oracle database to connect to (a free Oracle XE or a Docker image works fine) and the Oracle Instant Client. Detailed setup instructions for Windows and Linux are coming in Part 0.