Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:07:13+00:00 2026-05-25T11:07:13+00:00

I have created a MySQL table where one of the columns stores a BLOB

  • 0

I have created a MySQL table where one of the columns stores a BLOB type. (The Internet told me BLOB is the correct data type for images.)

I am pretty much a beginner with both C++ and MySQL. What I would like to do is to write a small program with a main() that puts a jpeg into that table. For the sake of this exercise, I do not want to store a reference to a directory that contains an image.

Am I wrong to think that it is as simple as filling out the part in BLOCK 2 below?

#include <iostream>
#include <string>
#include <mysql.h>

using namespace std;

int main(int argc, char **argv)
{

    //BLOCK 1: INIT
    MYSQL *connection, mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;

    int query_state;

    mysql_init(&mysql);
    connection = mysql_real_connect(&mysql, "localhost", "root", "secret", "beginner_db",0,0,0);

    //BLOCK 2: SEND QUERY
    /* do something to insert image to table */

    //BLOCK 3: DISPLAY QUERY RESULTS
    result = mysql_store_result(connection);
    /* do something with result */

    //BLOCK 4: FREE
    mysql_free_result(result);
    mysql_close(connection);

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:07:14+00:00Added an answer on May 25, 2026 at 11:07 am

    For this scenario, a good solution would be to use the mysql_stmt_send_long_data() function.

    There is an example on the MySQL Manual page that I linked to, but here is a more relevant example of sending file contents:

    #ifdef _WIN32
    #include <windows.h>
    #endif
    
    #include <cstddef>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    
    #include <boost/scope_exit.hpp>
    
    #include <mysql.h>
    
    #define ARR_LEN(arr_id) ((sizeof (arr_id))/(sizeof (arr_id)[0]))
    
    int main()
    {
        using namespace std;
    
        MYSQL *pconn = mysql_init(NULL);
        BOOST_SCOPE_EXIT( (pconn) ) {
            mysql_close(pconn);
        } BOOST_SCOPE_EXIT_END
    
        const char *db_name = "test";
        if (!mysql_real_connect(pconn, "localhost", "test", "********", db_name, 0, NULL, CLIENT_COMPRESS)) {
            cerr << "Error: mysql_real_connect() failed to connect to `" << db_name << "`." << endl;
            return EXIT_FAILURE;
        }
    
        MYSQL_STMT *pinsert_into_images_stmt = mysql_stmt_init(pconn);
        BOOST_SCOPE_EXIT( (pinsert_into_images_stmt) ) {
            mysql_stmt_close(pinsert_into_images_stmt);
        } BOOST_SCOPE_EXIT_END
    
        const char sql1[] = "INSERT INTO images(data) VALUES (?)";
        if (mysql_stmt_prepare(pinsert_into_images_stmt, sql1, strlen(sql1)) != 0) {
            cerr << "Error: mysql_stmt_prepare() failed to prepare `" << sql1 << "`." << endl;
            return EXIT_FAILURE;
        }
    
        MYSQL_BIND bind_structs[] = {
            { 0 } // One for each ?-placeholder
        };
    
        unsigned long length0;
        bind_structs[0].length = &length0;
        bind_structs[0].buffer_type = MYSQL_TYPE_BLOB;
        bind_structs[0].is_null_value = 0;
    
        if (mysql_stmt_bind_param(pinsert_into_images_stmt, bind_structs) != 0) {
            cerr << "Error: mysql_stmt_bind_param() failed." << endl;
            return EXIT_FAILURE;
        }
    
        const char *file_name = "image.jpg";
        FILE *fp = fopen(file_name, "rb");
        BOOST_SCOPE_EXIT( (fp) ) {
            fclose(fp);
        } BOOST_SCOPE_EXIT_END
    
        // Use mysql_stmt_send_long_data() to send the file data in chunks.
        char buf[10*1024];
        while (!ferror(fp) && !feof(fp)) {
            size_t res = fread(buf, 1, ARR_LEN(buf), fp);
            if (mysql_stmt_send_long_data(pinsert_into_images_stmt, 0, buf, res) != 0) {
                cerr << "Error: mysql_stmt_send_long_data() failed." << endl;
                return EXIT_FAILURE;
            }
        }
    
        if (!feof(fp)) {
            cerr << "Error: Failed to read `" << file_name << "` in its entirety." << endl;
            return EXIT_FAILURE;
        }
    
        if (mysql_stmt_execute(pinsert_into_images_stmt) != 0) {
            cerr << "Error: mysql_stmt_execute() failed." << endl;
            return EXIT_FAILURE;
        }
    
        cout << "Inserted record #" << mysql_insert_id(pconn) << endl;
        return EXIT_SUCCESS;
    }
    

    I am using the following definition of table `images`:

    CREATE TABLE images (
        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        data MEDIUMBLOB NOT NULL,
    
        PRIMARY KEY (id)
    );
    

    Upon running this program, it successfully sent the 38,339-byte JPEG image.jpg to the server and outputted “Inserted record #1”.

    You can verify that the correct number of bytes were sent:

    mysql> SELECT octet_length(data) FROM images WHERE id=1;
    +--------------------+
    | octet_length(data) |
    +--------------------+
    |              38339 |
    +--------------------+
    1 row in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one problem... There is a table in my MySQL database that stores
I have a huge table from one mysql db, I want to create a
I have created a MySQL table and would like to save the contents of
I have created mysql to delete table rows, which have lower DATETIME value than
I have created a test table in MySQL and would like to insert 10
i have a table in mysql db ( created_date). It stores timestamps (INT value).
I have this in a MySQL db: table Message sender_id int recipient_id int created
I'm doing a mysql table and i have one column that has current timestamp
I have a simple mysql table with five columns: id: auto_increment (primary) id_symbol: INT(4)
I have a table in mysql: ( _table ) with a a few columns,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.