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

后端 未结 2 1921
暖寄归人
暖寄归人 2021-02-10 05:44

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

2条回答
  •  一向
    一向 (楼主)
    2021-02-10 06:31

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

提交回复
热议问题