仅使用SQL将Blob从MySQL数据库导出到文件

发布于 2021-02-02 16:16:32

我有一个表,其中的图像数据存储在MySQL数据库的blob字段中。有没有一种方法可以仅使用SQL将这些图像导出到文件系统上的文件?图片应命名为{imageId}
.jpg

我知道用Java或其他方法很容易做到这一点,但是仅用SQL脚本就能做到吗?

关注者
0
被浏览
127
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    我不喜欢这个主意…

    drop procedure if exists dump_image;
    delimiter //
      create procedure dump_image()
      begin
    
        declare this_id int;
        declare cur1 cursor for select imageId from image;
        open cur1;
          read_loop: loop
            fetch cur1 into this_id;
            set @query = concat('select blob_field from image where imageId=', 
                this_id, ' into outfile "/tmp/xyz-', this_id,'.jpg"');
            prepare write_file from @query;
            execute write_file;
          end loop;
        close cur1;
      end //
    delimiter ;
    

    尽管有错误

    mysql>调用dump_image();
    错误1329(02000):无数据-提取,选择或处理了零行
    
    
    
    ls -1 / tmp / xyz *
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看