RAW data
Objectives — by the end of this page you will be able to:
- fetch and bind
RAWcolumns by index and by name - size your buffer correctly (
GetDataSizefirst) - hex-dump binary values safely
- use
ocilib::Raw(astd::vector<unsigned char>) in C++ - know when RAW vs BLOB vs LONG RAW applies
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]);
Raw buf;
st.Execute("select hextoraw('DEADBEEF') from dual");
if (st.GetResultset().Next())
{
buf = st.GetResultset().Get<Raw>(0); /* std::vector<unsigned char> */
}
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 */
}
rs.Next();
UInt32 len = rs.GetDataSize(0);
if (len <= 64)
{
Raw key = rs.Get<Raw>(0); /* sized automatically in C++ */
}
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");
}
static void hex_dump(const Raw& buf)
{
for (size_t i = 0; i < buf.size(); i++)
printf("%02X%s", buf[i], (i % 16 == 15) ? "\n" : " ");
if (buf.size() % 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"));
st.Execute("select rawtohex(session_key) from api_keys");
Binding
unsigned char key[16] = { /* … */ };
OCI_StatementBindRaw(st, OTEXT(":k"), key, sizeof(key));
Raw key{ /* … */ };
st.Bind(":k", 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 */
Raw digest(64);
st.Prepare("begin :d := my_hash(:s); end;");
st.Bind(":d", digest);
st.Bind(":s", secret);
st.ExecutePrepared(); /* 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:
- Size first.
OCI_ResultsetGetDataSizegives the byte length before you commit a buffer — essential when the column's width isn't under your control. - Fetch into a caller buffer.
GetRawtakesbuffer + capacityin and handsbytes-writtenout — no handle, no ownership, just your array. - 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
- Store a 16-byte UUID in a
RAW(16)column and read it back. - Fetch
sys_guid()— a RAW — and hex-print it. - Compare
OCI_ResultsetGetRawbehavior when the column is longer than your array vs afterGetDataSizepre-sizing. - Fetch
rawtohex(sys_guid())as text and compare with your own dump.
Common pitfalls
- Buffer too small —
GetRawneeds capacity ≥ data; checkOCI_ResultsetGetDataSizefirst. - Treating RAW as string — it's not NUL-terminated or printable; hex-dump it.
- Forgetting
lenis in/out — pass capacity in, read bytes-written out. - RAW in PL/SQL OUT binds — same
OCI_StatementBindRaw+ buffer rule.
Recap
- RAW = fixed small binary; bind/get via byte buffers;
ocilib::Rawin C++;GetDataSizefor unknown lengths;rawtohexfor display.
Next: OCI_Vector — embeddings for AI Vector Search (23ai).