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?

| improve this question | | | | |
  • $\begingroup$ Try SQLBinary as described in reference.wolfram.com/mathematica/DatabaseLink/tutorial/… $\endgroup$ – Sjoerd C. de Vries Feb 25 '13 at 8:20
  • $\begingroup$ @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... $\endgroup$ – Albert Retey Feb 25 '13 at 8:53
  • 1
    $\begingroup$ Yes, there is an example in the docs that does this exactly reference.wolfram.com/mathematica/DatabaseLink/ref/… $\endgroup$ – Ajasja Feb 25 '13 at 9:28
  • $\begingroup$ @Ajasja how's that different than what I pointed to above? $\endgroup$ – Sjoerd C. de Vries Feb 25 '13 at 18:00
  • $\begingroup$ @SjoerdC.deVries Not at all, I just did not scroll far enough to see it. no more late night mma.se for me:) $\endgroup$ – 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"]];

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.

| improve this answer | | | | |
  • $\begingroup$ Thanks for your good solution :) $\endgroup$ – Darren Lee Feb 26 '13 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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