LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I convert a cluster into a string

I am looking to store a cluster to a database. The database is mysql, I need to find away to convert the cluster into a form that can be stored in the database.
0 Kudos
Message 1 of 11
(5,001 Views)
It depends on what's in the cluster. The best general approach is probably to use Unbundle or Unbundle by Name to get to the individual elements, then pass the elements into Format Into String, the output of which you can send to MySQL.

It will be more complicated if there are arrays or complicated data types inside the cluster.

Steven H.
0 Kudos
Message 2 of 11
(4,987 Views)
Hello,

Try the function "Flatten to String" from the "Advanced" -- "Data Manipulation" group, this converts anything into a binary string.
Then you can use the "Unflatten from String" to do the reverse.

Hope this helps,
Paulo
0 Kudos
Message 3 of 11
(4,987 Views)
Be careful with Flatten to String. You do get a string out, but that string contains the raw (binary) representation of the data. It's not likely to be formatted the way you'd want if your goal is to send it to MySQL. To see this, create a simple cluster containing a numeric and a string, wire it to Flatten to String, and examine the output in both Normal and Hex format. It's not very friendly for this purpose.

Steven H.
0 Kudos
Message 4 of 11
(4,967 Views)
the flatten string looks like it might work...how about converting it to xml and then storing it?
0 Kudos
Message 5 of 11
(4,963 Views)
When I suggested the Flatten to String I was assuming that the data was only to be Stored/Read from LabVIEW, if so, I thing the method is ok (also the Flatten to XML), if not, is you want to be able to read it from, for insatnce the MySQL Query Browser, or you want to use it as a field to include in Where clauses, then this is not a good procedure, because then the string will not be readable.
You allways have to perform "Unflatten" before using it.
0 Kudos
Message 6 of 11
(4,955 Views)
it will only be used to store/read from labview. Thank you for all the help.
0 Kudos
Message 7 of 11
(4,948 Views)
Be very careful with flatten to string. If you don't know the exact format of the data, you will not be able to decode it. Experience has shown that storing flattened strings in databases is usually a bad idea, as the format tends to get lost, leaving you with worthless data. It's more work to convert it to human readable strings, but will be worth it in the long run. If you do go the flattened string route, make sure you have a format version number stored as well, so you can change the data format and still decode it. This is another reason to convert it yourself - partial reading of a changed format with old code is still possible (if you think about it up front and design for it).

Flatten to XML solves some of these problems but leaves others. The major problem is conversion of numerics. The format is fixed and cannot be changed. If it works for you - great. If not - use something else.

Take home message - if you want a robust, long-term solution, parse the data into text fields for the database. If you want a quick-and-dirty short-term solution, use flatten to string or XML.
Message 8 of 11
(4,933 Views)
I am having another issue. When I use flatten to string I get some odd characters that mysql will not except as a varchar. Am I doing something wrong?
0 Kudos
Message 9 of 11
(4,927 Views)
You've run across the limitation I mentioned above with Flatten To String: what you get out is the binary representation of what you passed in. In isn't formatted to be human-readable and it may well contain bytes that a database won't like. Some databases define a "blob" type that will accept any stream of bytes; varchar is apparently more picky.

See my previous suggestion about unbundling and using Format Into String.

Steven H.
0 Kudos
Message 10 of 11
(4,917 Views)