Storing arrays as Blobs in MySQL with C++

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
Greetings,

I need to store 2D and 3D arrays of integers and floats (respectively) in a mysql database. Each of these arrays is associated with a single tuple. I assume that I would need to somehow serialize these things and store them as a blob.

Now, I am using Sun's mySQL C++ library for C++. The setBlob preparedstatement function accepts an istream.

My first thought was to play around with casting and memcpy to dump the bytes from the arrays into 1D character vectors and use the istringstream to istream to beam it up to that database, that failed.

Currently, I write all of the values to an EOL delimited text file and use an istream in the form of an ifstream to upload the contents, but that is very lame and I would like to bypass the writing to a file.

I was considering using pipes, but not sure if that was the best way to go about doing it either.

Any thoughts?

Oh, this is using the Sun Studio compiler in OpenSolaris, so BOOST and other mainstream libraries will not work.
 

Train

Lifer
Jun 22, 2000
13,583
80
91
www.bing.com
I'm not sure how mySQL does it, but SQL Server usually accepts blobs as a single byte array.

What failed? did you get an error message when you tried to use the setBlob method?
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
What failed? did you get an error message when you tried to use the setBlob method?

The setblob function only accepts istreams. When I tried to memcpy the float array into a character array (adjusted the malloc to compensate for the differing size) and then initialized a string with it, the resulting string was empty.
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
Well, I can bypass the text file at least, still seems inefficient since I am basically storing the float array as a string inside a blob, ack...

Now I do the following and it works (just takes up more space):
1. Traverse the float array and dump the contents into an ostringstream, separated by a space
2. Open create an istringstream from the ostringstream
3. Set the istream to the setBlob preparedstatement from the istringstream
4. Execute
 

ForumMaster

Diamond Member
Feb 24, 2005
7,792
1
0
just asking, instead of storing the array in a blob, which is inefficient (as the file isn't actually a binary array, but an array of floats and integers), why not create a new type of array and dump your stuff things there?

you can do this in oracle, i'm sure you can do it MySql. you simply create a new array type of let's say, float, then insert your object there. add a column of this type. it will be more efficient.
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
why not create a new type of array and dump your stuff things there?

you can do this in oracle, i'm sure you can do it MySql.

you simply create a new array type of let's say, float, then insert your object there
Any link to a guide on how to do this in mysql? A quick googling turned up nothing. I did not know it was possible. Keep in mind, it needs to work with the C++ mysql API.

Thanks