Create the field with MEDIUMBLOB type.
Size of MEDIUMBLOB is adequate normally.
Assign the value of the field with STRTOHEX( cImageBuffer )
You can read straight without any conversion.
If you use ADO and if you have recent versions of FWH, then FWH makes all this process extremely simple.
Here is an example.
- Code: Select all Expand view RUN
#include "FiveWin.Ch"
#include "xbrowse.ch"
#include "adodef.ch" // IMPORTANT
function mysqlimages()
local oCn, oRs, cSql, a
local cPath := "c:\fwh\bitmaps\pngs\"
local cPassWord := <yourpasswordhere>
oCn := FW_OpenAdoConnection( { "MYSQL", "localhost", "FWH", "root", cPassWord } )
if FW_AdoTableExists( "IMAGETEST", oCn )
oCn:Execute( "DROP TABLE IMAGETEST" )
endif
FWAdoCreateTable( "IMAGETEST", ;
{ { "IMGNAME", 'C', 20, 0 }, ;
{ "IMAGE", 'm', 10, 0 } }, ; // small 'm' indicates binary data
oCn )
for each a in Directory( cPath + "*.png" )
cSql := SQL ; // SQL is FWH command
INSERT INTO IMAGETEST ( IMGNAME, IMAGE ) ;
VALUES ( a[ 1 ], MemoRead( cPath + a[ 1 ] ) )
oCn:Execute( cSql )
next
oRs := FW_OpenRecordSet( oCn, "IMAGETEST" )
XBROWSER oRs TITLE "IMAGES IN MYSQL" SETUP ( ;
oBrw:nEditTypes := EDIT_GET, ;
oBrw:lCanPaste := .t. )
oRs:Close()
oCn:Close()
return nil
Advantage of using FWH functions and commands is that we can use the same code for Access, MsSql, MySql, SQLite, etc. FWH takes care of the differences in different DBMSs.
Incidentally XBrowse and TDataRow classes are excellent editors too and can handle the complexities of image data transparently.
For example, in the above example, you can copy an image from web or other sources and paste into the image cell of xbrowse with Ctrl-V and the new image is written into the MySql table automatically.