# How can I write image to MySQL database?

I have a database, such as:

In Mathematica, the final code is:

img1=Export["/home/user/Desktop/1.jpg",resultimg1];


I tried SQLInsert[conn,"mathematica",{"image"},{img1}];

but the MySQL result is:

How can I write the image to MySQL database?

-
Try SQLBinary as described in reference.wolfram.com/mathematica/DatabaseLink/tutorial/… –  Sjoerd C. de Vries Feb 25 '13 at 8:20
@user5990: Export returns the full path to the file it wrote and that's what you are inserting into your database. If, as I guess, you want to write the file content instead of the filename you could e.g. use ExportString and ToCharacterCode to get a binary representation of the file content to insert into your database... –  Albert Retey Feb 25 '13 at 8:53
Yes, there is an example in the docs that does this exactly reference.wolfram.com/mathematica/DatabaseLink/ref/… –  Ajasja Feb 25 '13 at 9:28
@Ajasja how's that different than what I pointed to above? –  Sjoerd C. de Vries Feb 25 '13 at 18:00
@SjoerdC.deVries Not at all, I just did not scroll far enough to see it. no more late night mma.se for me:) –  Ajasja Feb 25 '13 at 21:03

Export returns the filename to which your data was exported. Your code does insert that filename into your database, and you see that this actually does work. What you most probably want is to insert the file content, not the filename, into the database. For that you need a combination of ExportString, ToCharacterCode and SQLBinary. This is described, as Ajasja has mentioned, in the documentation for SQLBinary. Here is how it would look like for your example:

filecontent = ToCharacterCode[ExportString[resultimg1,"JPEG"]];
SQLInsert[conn,"mathematica",{"image"},{SQLBinary[filecontent]}];


Depending on how you are going to read the data you might need to take care of the correct encoding, if you read and write both from the same machine with Mathematica I believe the defaults should work alright.

-
Thanks for your good solution :) –  user5990 Feb 26 '13 at 5:45