简体   繁体   中英

Joining binary data from blobs in mysql

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. Eg to read it into Audacity you would go to import->raw.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM