0

I have a mysql database table with audio data in MEDIUMBLOB fields. The thing is, one audio file is in different rows. So I want to join them. When I do this:

select data from audio where id=1 into outfile "output" fields escaped by '';

.. I get audio. When I do the same thing for id=2, I get audio. When I put them together:

select data from audio order by date, time into outfile "output" fields escaped by '';

.. I get audio for awhile, then high amplitude noise. The noise starts where id=2 would have been. If I select more than two columns to put together, sometimes the output from that particular id is noise, sometimes it's the correct audio. It's not exactly interleaved.

So, how can I extract and concatenate blobs from multiple rows into a coherent binary output file?

Edit: this is raw audio data. E.g. to read it into Audacity you would go to import->raw.

2
  • why type of 'audio' file is contained within the binary data? Commented Dec 28, 2014 at 19:24
  • @Steve - raw audio, 16-bit unsigned Commented Dec 28, 2014 at 19:38

2 Answers 2

1

SELECT INTO OUTFILE is intended mainly to allow you quickly dump a table to a text file, and also to complement LOAD DATA INFILE.

I strongly suspect that the SELECT INTO OUTFILE is either adding an ASCII NULL, or other formatting between columns so that it can be read back.

You could compare binary output to determine if this is true, and also pick up upon any other formatting, encoding or shaping that may also be present.

Do you have to use INTO OUTFILE? - Can you not get the binary data and create a file directly with a script or other middle tier layer rather than relying on the database?

Update

After writing this, and reading the comment below. I thought about CONCAT, MySQL treats BLOB values as binary strings (byte strings), therefore in theory a simple concatenation of multiple columns into one single one might work.

If it doesn't then it wouldn't take much to write a simple pearl, PHP, C, bash or other scripting language to query the database, join two or more binary columns and write the output to a file on the system.

Sign up to request clarification or add additional context in comments.

4 Comments

I am using 'fields escaped by' because I had a problem like you described before. I was wondering if anything else can help me (x escaped by...). Do you mean an sql script to do it? Or another programming language?
@horsehair i've added an update - was a bit too large for a comment
concat would not work because it contacts columns and not rows. he wants the same column, in different rows, concatenated.
@Born2Code concat would work because you can easily join multiple rows together via sub query or a cross tabulation (pivot) query
1

what you need is to run the mysql command from the command line, using -e to pass a sql statement to execute, and the appropriate flags for the binary output.

You then use group_concat with an empty separator to concat all the data.

Finally, you output to your file. The only thing you need to be aware of is the built in limit on the group_concat, which you can change in your .ini file, or by setting a global variable.

Here is how would this look:

mysql --binary-mode -e "select group_concat(data separator '') from audio order by date, time;" -A -B -r -L -N YOUR_DATABASE_NAME > output

i just tried it with binary data (not audio) in a database and it works.

4 Comments

this looks promising except the output is always 1025 long, though I verify through the mysql command line that the length of the 'data' fields are much greater than that.
that's the default for the group_concat_max_len, as i noted in my answer, you will need to increase this value, either in your .ini file or by setting the global variable @@group_concat_max_len see dev.mysql.com/doc/refman/5.1/en/… for more details
If it does then you got the wrong answer marked as correct :)
I wish I could mark them both. They both had the same result but in the end I did it with a Python script.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.