OCILIB Tutorials v5.0.0
Beginner

RAW data

Objectives — by the end of this page you will be able to:

In context — bytes without interpretation

RAW is Oracle's "no-questions-asked" binary column: the database stores and returns the exact bytes you gave it, with no charset conversion — which is the key difference from VARCHAR2. A string column is interpreted (the server may transcode it between client and database charsets); a RAW column is opaque. That makes RAW the canonical home for machine data that must survive unchanged: UUIDs (SYS_GUID() returns RAW(16)), hash digests, encryption keys, packed binary structures, foreign-system identifiers.

The size ceiling (~2000 bytes in-row, 32767 via PL/SQL on modern versions) is also the dividing line between the three binary families: RAW for small fixed data, BLOB for content you stream, LONG RAW for legacy columns you are migrating away from.

Because there is no character data inside, OCILIB exposes RAW as plain buffer + length rather than as a handle: void* + OCI_UInt32 in C, ocilib::Raw (a std::vector<unsigned char>) in C++. The length is in/out everywhere — capacity in, bytes-written out — and OCI_ResultsetGetDataSize is the way to pre-size a buffer for a column whose width you do not control.

Concepts

RAW = small binary (≤ 2000 bytes in-table, 32767 in PL/SQL/SQL on modern versions). GUIDs, hashes, keys, packed structs live here.

It's not a stream like a LOB — just bytes in a buffer, fetched through a raw getter into your array. There's no OCI_Raw object: the C API takes void* + OCI_UInt32 and returns bytes written; C++ wraps that as Raw.

C function C++ method Purpose
OCI_ResultsetGetRaw Get<Raw>(i) fetch → caller buffer / Raw
OCI_ResultsetGetRawByName Get<Raw>("col") fetch by name
OCI_ResultsetGetDataSize GetDataSize(i) column byte length
OCI_StatementBindRaw Bind(":r", raw) bind buffer + size

Fetching

unsigned char buf[256];      /* must hold the column's byte size     */

OCI_ConnectionImmediate(cn, OTEXT("select hextoraw('DEADBEEF') from dual"),
              OCI_ARG_RAW, buf);         /* bytes copied = column size */
printf("got 4 bytes: %02X%02X…\n", buf[0], buf[1]);

In a resultset: OCI_ResultsetGetRaw(rs, i, buf, &len) — len is in = capacity, out = bytes written.

Scenario — unknown-length column

GetDataSize tells you the real length before you commit a buffer:

OCI_ResultsetFetchNext(rs);

OCI_UInt32    len = OCI_ResultsetGetDataSize(rs, 0);
unsigned char key[64];

if (len <= sizeof(key))
{
    OCI_ResultsetGetRaw(rs, 0, key, &len);
}
else
{
    /* column is longer than expected — handle/allocate accordingly */
}

Scenario — hex-dumping

RAW isn't printable — write a tiny hex helper for logs:

static void hex_dump(const unsigned char *buf, OCI_UInt32 len)
{
    for (OCI_UInt32 i = 0; i < len; i++)
        printf("%02X%s", buf[i], (i % 16 == 15) ? "\n" : " ");
    if (len % 16) printf("\n");
}

Or let the server do it — rawtohex(col) in SQL gives you text:

OCI_StatementExecute(st, OTEXT("select rawtohex(session_key) from api_keys"));

Binding

unsigned char key[16] = { /* … */ };
OCI_StatementBindRaw(st, OTEXT(":k"), key, sizeof(key));

Scenario — PL/SQL OUT binds

An OUT RAW parameter needs the same buffer rule — capacity in, bytes out:

unsigned char digest[64];
OCI_UInt32    dlen = sizeof(digest);

OCI_StatementPrepare(st, OTEXT("begin :d := my_hash(:s); end;"));
OCI_StatementBindRaw   (st, OTEXT(":d"), digest, dlen);
OCI_StatementBindString(st, OTEXT(":s"), (otext*) secret, 0);
OCI_StatementExecutePrepared(st);          /* digest filled on return */

When to use what

SQL type Size How to access
RAW ≤ 32 KB OCI_ResultsetGetRaw/OCI_StatementBindRaw — simple buffer
LONG RAW huge legacy OCI_Long piecewise (page 285)
BLOB huge OCI_Lob streaming (page 550)

Putting it together — the typical sequence

RAW code is a three-step pattern, same every time:

  1. Size first. OCI_ResultsetGetDataSize gives the byte length before you commit a buffer — essential when the column's width isn't under your control.
  2. Fetch into a caller buffer. GetRaw takes buffer + capacity in and hands bytes-written out — no handle, no ownership, just your array.
  3. Display or use: hex-dump it (helper above) or let SQL do it with rawtohex(col) when you want text.

Binding is symmetric — StatementBindRaw with an explicit byte count — and a PL/SQL OUT RAW parameter is just a bind whose buffer comes back filled.

Hints: RAW data is never NUL-terminated — always use the returned byte count, never strlen; over ~2000 bytes switch to BLOB and the LOB streaming pattern; and if you're storing a GUID, SYS_GUID() already returns RAW(16) — fetch it, don't generate your own.

Try it yourself

  1. Store a 16-byte UUID in a RAW(16) column and read it back.
  2. Fetch sys_guid() — a RAW — and hex-print it.
  3. Compare OCI_ResultsetGetRaw behavior when the column is longer than your array vs after GetDataSize pre-sizing.
  4. Fetch rawtohex(sys_guid()) as text and compare with your own dump.

Common pitfalls

Recap

Next: OCI_Vector — embeddings for AI Vector Search (23ai).