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 906719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:26:06+00:00 2026-05-15T16:26:06+00:00

I am using the MySql Connector C++ to store a JPEG image from a

  • 0

I am using the MySql Connector C++ to store a JPEG image from a file into the database. I am using the prepared statement. After execution of the prepared statement, only the first 64 bytes of the file are copied into the database.

My research of examples show that no iteration is necessary and the examples assume that the prepared statement loads the entire file. Here is my code:

std::string         statement_text("INSERT INTO ");
statement_text += "picture_image_data";
statement_text += " (";
statement_text += "ID_Picture";
statement_text += ", ";
statement_text += "Image_Data";
statement_text += ") VALUES (?, ?)";    
wxLogDebug("Creating prepared statement using:\n%s\n", statement_text.c_str());
std::string filename("my_image.jpg");
Ptr_Db_Connection                           db_conn(db_mgr.get_db_connection());
boost::shared_ptr<sql::PreparedStatement>   prepared_statement(db_conn->prepareStatement(statement_text));
prepared_statement->setInt(1, picture_id);
std::ifstream   blob_file(filename.c_str());
prepared_statement->setBlob(2, &blob_file);
prepared_statement->execute();
blob_file.close();

Here is the schema of the table:

mysql> describe picture_image_data;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| ID_Picture | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Image_Data | mediumblob       | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

From the MySql Console:

mysql> select ID_Picture, LENGTH(image_data)
    -> FROM picture_image_data
    -> where ID_Picture = 1;
+------------+--------------------+
| ID_Picture | LENGTH(image_data) |
+------------+--------------------+
|          1 |                 65 |
+------------+--------------------+
1 row in set (0.02 sec)

How do I make the prepared statement read the entire file?
Do I need to initialize anything in MySql Connector C++ to make this read more than 64 bytes?

Note: I am using MySql Connector C++ 1.0.5, Visual Studio 2008 and wxWidgets on Windows XP and Vista.

Table creation statement:

CREATE TABLE Picture_Image_Data
(
    ID_Picture INTEGER UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
    Image_Data MEDIUMBLOB
);

Hex dump (via Cygwin) of the image file:

0000000 d8ff e0ff 1000 464a 4649 0100 0101 1c00
0000010 1c00 0000 dbff 4300 0500 0403 0404 0503
0000020 0404 0504 0505 0706 080c 0707 0707 0b0f
0000030 090b 110c 120f 1112 110f 1311 1c16 1317
0000040 1a14 1115 1811 1821 1d1a 1f1d 1f1f 1713
0000050 2422 1e22 1c24 1f1e ff1e 00db 0143 0505
0000060 0705 0706 080e 0e08 141e 1411 1e1e 1e1e
0000070 1e1e 1e1e 1e1e 1e1e 1e1e 1e1e 1e1e 1e1e  
  • 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-15T16:26:07+00:00Added an answer on May 15, 2026 at 4:26 pm

    The issue lies in the constructor of the image file:

    std::ifstream   blob_file(filename.c_str());
    

    This should have the binary mode attribute:

    std::ifstream   blob_file(filename.c_str(), std::ios_base::binary);
    

    The file, a JPEG image, is binary data.

    Also, the hex dump at byte 65 shows 1a, which is the Windows OS end of file character:
    0000040 1a14 1115 1811 1821 1d1a 1f1d 1f1f 1713

    After fixing the constructor, the MySql shows the data size:

    mysql> SELECT ID_Picture, LENGTH(Image_Data)
        -> FROM picture_image_data
        -> WHERE ID_Picture = 1;
    +------------+--------------------+
    | ID_Picture | LENGTH(Image_Data) |
    +------------+--------------------+
    |          1 |              18453 |
    +------------+--------------------+
    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

How do I connect to the database(MYSQL) in connection bean using JSF to retrieve
I've connected to a MySQL database using Perl DBI. I would like to find
I am trying to connect to a remote MySQL database using Visual C# 2008
Using MySQL , I can do something like: SELECT hobbies FROM peoples_hobbies WHERE person_id
How do I connect to a MSSQL database using Perl's DBI module in Windows?
Using MySQL syntax and having a table with a row like: mydate DATETIME NULL,
Using MySQL syntax, how would I write a query to return the following (I
Note: Using MySQL 4.0, which means no subqueries (at present). I have 2 tables:
I'm using MySQL in particular, but I'm hoping for a cross-vendor solution. I'm using
I am using MySQL and PHP for a project I am working. I have

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.