Click here to Skip to main content
15,112,333 members
Articles / Database Development / MySQL
Article
Posted 27 May 2008

Stats

43.6K views
2K downloads
17 bookmarked

Upload images into a MySQL database using VC++

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
24 Sep 2009CPOL1 min read
How to upload images into a MySQL BLOB field using the ODBC driver.

Introduction

This article explains uploading image files into a MysQL database using the MySQL C/C++ libraries or MySQL ODBC drivers. When I try to upload image files into MySQL, I was able to read it as bytes or characters but I couldn't insert an image into a MySQL BLOB field using the ExecuteSQL function in the CDatabase class.

Image files contain special characters especially escape sequences. If we want to upload an image file, read it as a string or character constants, and replace all (\) with (\\). My library does this as well. My library function converts unformatted data into formatted data. We can then easily upload it to a MySQL database.

Using the code

The downloadable zip file contains...

  1. mysqlfileimportexport.dll
  2. mysqlfileimportexport.lib
  3. mysqlfileimportexport.h

The following code snippet describes how to use mysqlfileimportexport.dll.

Class name: Cmysqlfileimportexport

Method: blob_Import

  • Step 1: Include this header file and library file into your code.
  • C++
    #include "mysqlfileimportexport.h"   
    #pragma comment(lib,mysqlfileimportexport.lib)
  • Step 2: Write the code for your database connection.
  • C++
    CDatabase db_Obj;
    
    //Database connection code here
    db.Obj.OpenEx(...)
  • Step 3: Include these lines into your code:
  • C++
    Cmysqlfileimportexport lib_Obj; 
    LPSTR Buffer = NULL;
    int iReturnValue = lib_Obj.blob_Import("c:\\xx\\yyy.abc",&Buffer);
    //If return 0 success, otherwise failiure.
    
    CString sQuery;
    sQuery.Format("INSERT INTO tablename fieldname(LONGBLOB) VALUES('%s')", Buffer);
  • Step 4: Execute the query using the database connection variable.
  • C++
    db_Obj.ExecuteSQL(sQuery);
    ...

Note

Don't forget to add mysqlfileimportexport.dll in your project directory.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

$uresh $hanmugam
Software Developer
India India
Working in SIEMENS, Chennai, India - currently into vc++.

Comments and Discussions

 
QuestionBinary data of the image changes? Pin
Gokulnath00715-Jul-11 3:06
MemberGokulnath00715-Jul-11 3:06 
Binary data of the image changes? So, we will not get the original image, it is utter waste. If a single space is inserted in the binary data of the image, then the originality changes and the original image will not be reproduced. I am damn sure about this issue. Is there a way to get the original image then..,,,,,,,,,
GeneralThis is not an article Pin
transoft25-Sep-09 3:37
Membertransoft25-Sep-09 3:37 
Questionhow to read a file ? Pin
lanis120111-Nov-08 8:31
Memberlanis120111-Nov-08 8:31 
AnswerRe: how to read a file ? Pin
jacksp29-Sep-09 0:22
Memberjacksp29-Sep-09 0:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.