Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert plot into mysql from Mathematica as blob

In php you can insert pictures into database as blob data type. I'd like to put plots of data into a database as a blob datatype so I can reuse this data on the web with php.
For example:

Needs["DatabaseLink`"];
conn=OpenSQLConnection["myDB"];
picture=Plot[Sin[x], {x,0,2Pi}];
SQLExecute[conn, "INSERT INTO pictures VALUES ('"<>picture<>"')"]
like image 241
enedene Avatar asked Feb 22 '26 15:02

enedene


1 Answers

There is an example in V 8.0.4 documentation page SQLBinary that gives the steps needed. After you export picture to a format of your choice as @halirutan and @Verbeia suggested, say

img=ExportString[picture, "PNG"]; 

you need to convert the resulting string to raw binary data using

byteData=SQLBinary[ToCharacterCode[img]];

Assuming the column pictures (with datatype LONGVARBINARY, VARBINARY or BINARY) lives in, say, PICTABLE then insert byteData into the pictures column using

SQLInsert[conn, "PICTABLE", {"pictures"}, {byteData}];

or equivalent raw SQL command inside SQLExecute[ ... ].

like image 60
kglr Avatar answered Feb 26 '26 04:02

kglr