Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a database, such as: Database schema

In Mathematica, the final code is:


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

but the MySQL result is:

Example with just image name inserted

How can I write the image to MySQL database?

share|improve this question
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

1 Answer 1

up vote 2 down vote accepted

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"]];

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.

share|improve this answer
Thanks for your good solution :) –  user5990 Feb 26 '13 at 5:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.