2012-01-24 22 views

答えて

10

PostgreSQLの— BYTEALarge Objectsの2種類があります。大きなオブジェクトを使用することをお勧めしたいと思います。

BYTEAのためには、libpqの中で、このようなものを使用したい:libpqのはCのAPIである一方、

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; 
} 
1

使用libpqxxは、C++、それを行うための方法です。ここで

はpqxxを使用してそれを行う方法の完全な例です。このようになりlibpqxx使用して短い、関連するC++ラインでHow to insert binary data into a PostgreSQL BYTEA column using the C++ libpqxx API?

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(); 
+1

libpqxxと高齢化問題がある注目に値します、いいえ、それは "公式" postgresql C++ apiではなく、サイト上にあります。 – user3791372

関連する問題