Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Binary Large Object (BLOB) in PostgreSQL using libpq from remote machine

Can you give an example of inserting binary data in PostgreSQL database from remote machine using libpq. My second question is: Is there any other API more efficient than libpq with C++. Thanks

like image 591
Mezo Avatar asked Jan 24 '12 21:01

Mezo


People also ask

How can you store the binary data in PostgreSQL?

PostgreSQL provides two distinct ways to store binary data. Binary data can be stored in a table using the data type bytea or by using the Large Object feature which stores the binary data in a separate table in a special format and refers to that table by storing a value of type oid in your table.

Can we store BLOB in PostgreSQL?

Blob (Binary large object) is an Oracle data type that is used to store binary data like contents of a file or information like audio, video, and images. PostgreSQL does not have the Blob data type directly, but we can work with it using the methods below.

How do I declare a BLOB in PostgreSQL?

Add new column and define data type as blob/bytea. Below example shows that add new column and define data type as blob/bytea. In below example we are adding column name as blob_test1 and defining data type as blob/bytea for the same.

What is Bytea in PostgreSQL?

The bytea data type allows the storage of binary strings or what is typically thought of as “raw bytes”. Materialize supports both the typical formats for input and output: the hex format and the historical PostgreSQL escape format. The hex format is preferred.


2 Answers

There are 2 types of blobs in PostgreSQL — BYTEA and Large Objects. I'd recommend against using large objects as you can not join them to tables.

For BYTEA you'd use something like this in libpq:

PGresult* put_data_to_tablename(
  PGconn* conn,
  int32_t id,
  int data_size,
  const char* const data
) {
  PGresult* result;
  const uint32_t id_big_endian = htonl((uint32_t)id);
  const char* const paramValues[] = { &id_big_endian, data };
  const int nParams = sizeof(paramValues) / sizeof(paramValues[0]);
  const int paramLenghts[] = { sizeof(id_big_endian), data_size };
  const int paramFormats[] = { 1, 1 }; /* binary */
  const int resultFormat = 0; /* text */

  result = PQexecParams(
    conn,
    "insert into tablename (id, data) values ($1::integer, $2::bytea)",
    nParams,
    NULL, /* Types of parameters, unused as casts will define types */
    paramValues,
    paramLenghts,
    paramFormats,
    resultFormat
  );
  return result;
}
like image 119
Tometzky Avatar answered Oct 23 '22 04:10

Tometzky


Using libpqxx is the C++ way to do it, while libpq is the C API.

Here is a full example of how to do it using pqxx: How to insert binary data into a PostgreSQL BYTEA column using the C++ libpqxx API?

In short, the relevant C++ lines using libpqxx look like this:

void * bin_data = ...; // obviously do what you need to get the binary data...
size_t bin_size = ...; // ...and the size of the binary data
pqxx::binarystring bin( bin_data, bin_size );
pqxx::result r = work.prepared( "test" )( bin ).exec();
work.commit();
like image 3
Stéphane Avatar answered Oct 23 '22 06:10

Stéphane