简体   繁体   English

在mysql中从Blob连接二进制数据

[英]Joining binary data from blobs in mysql

I have a mysql database table with audio data in MEDIUMBLOB fields. 我在MEDIUMBLOB字段中有一个带有音频数据的mysql数据库表。 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. 当我对id = 2做同样的事情时,我得到音频。 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. 噪声从本应为id = 2的位置开始。 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. 如果我选择两个以上的列来组合,则有时该特定ID的输出是噪声,有时它是正确的音频。 It's not exactly interleaved. 它不是完全交错的。

So, how can I extract and concatenate blobs from multiple rows into a coherent binary output file? 那么,如何将多行中的Blob提取并连接到一个连贯的二进制输出文件中?

Edit: this is raw audio data. 编辑:这是原始音频数据。 Eg to read it into Audacity you would go to import->raw. 例如,要将其读入Audacity,您将转到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 . SELECT INTO OUTFILE主要用于使您快速将表转储到文本文件,并补充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. 我强烈怀疑SELECT INTO OUTFILE是在列之间添加ASCII NULL还是其他格式,以便可以将其读回。

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? 您必须使用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. 我想到了CONCAT ,MySQL将BLOB值视为二进制字符串(字节字符串),因此从理论上讲,将多个列简单连接成一个即可。

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. 如果不是这样,那么编写简单的Pearl,PHP,C,bash或其他脚本语言来查询数据库,联接两个或更多二进制列并将输出写入系统上的文件就不会花费太多。

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. 您需要从命令行运行mysql命令,使用-e传递要执行的sql语句以及二进制输出的相应标志。

You then use group_concat with an empty separator to concat all the data. 然后,使用带空分隔符的group_concat合并所有数据。

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. 您唯一需要了解的是group_concat的内置限制,您可以在.ini文件中更改该限制,也可以通过设置全局变量来更改该限制。

Here is how would this look: 这是什么样子:

mysql --binary-mode -e "select group_concat(data separator '') from audio order by date, time;" “ mysql --binary-mode -e”按日期,时间从音频顺序中选择group_concat(数据分隔符); -A -B -r -L -N YOUR_DATABASE_NAME > output -A -B -r -L -N YOUR_DATABASE_NAME>输出

i just tried it with binary data (not audio) in a database and it works. 我只是用二进制数据(不是音频)在数据库中尝试过,它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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