4
$\begingroup$

I have a database, such as: Database schema

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:

Example with just image name inserted

How can I write the image to MySQL database?

$\endgroup$
6
  • $\begingroup$ Try SQLBinary as described in reference.wolfram.com/mathematica/DatabaseLink/tutorial/… $\endgroup$ Feb 25, 2013 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$ Feb 25, 2013 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, 2013 at 9:28
  • $\begingroup$ @Ajasja how's that different than what I pointed to above? $\endgroup$ Feb 25, 2013 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, 2013 at 21:03

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thanks for your good solution :) $\endgroup$
    – Darren Lee
    Feb 26, 2013 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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