SQLite Forum

Confused about blobs
Login
> My question is which technique should I use, bind or open/write/read? the documentation about blobs seems to favor bind, but open write, read seems more intuitive. 

The open/write/read/close APIs provide the ability to incrementally write/read blob data (similar to a file handle) and are most useful for large blobs which you "really don't" want to hold entirely in client-side memory at once. The vast majority of the time, the `sqlite3_bind_blob()` API is easier, and `sqlite3_column_blob()` for reading.

Unless you know you have a need for the more detailed APIs, ignore them and stick with the bind/column APIs.

> `int myfunction_writeblob(char *image, long bytes_in_image) { ... } `

Here's an example which uses the open/read/write/close API:

<https://icculus.org/~chunky/stuff/sqlite3_example/sqlite3_example_blob.c>

but that's the hard way to do it. The easier way is (pseudocode):

```
// write:
sqlite3_bind_blob(myStmt, paramIndex, blobMemPtr, lenOfBlobMemPtr, SQLITE_TRANSIENT );
// ^^^ noting that SQLITE_STATIC may be better (use less memory),
// depending on the context, but SQLITE_TRANSIENT is safer, in
// terms of potentially memory misuse, if you're uncertain.
sqlite3_step(myStmt);

// read:
void const * blob = sqlite3_column_blob(myStmt, columnIndex);
int const blobLen = sqlite3_column_byte(myStmt, columnIndex);
```

That's all there is to it. If you know how to bind and read text columns, you already know how to bind and read blob columns. Don't let the different data type name fool you into thinking otherwise.