Dumping blob rows to files from MySQL

拟墨画扇 提交于 2020-01-06 07:26:51

问题


I have a table with a few thousand rows in it that has a few integer columns and a blob column. I want to dump each row out as its own file with the blob being the content and the integers being used to form the file name. This is a one time op so quick and dirty is OK. One constraint is that I have almost no tools installed in this enviornment so that will be part of the dev cost no matter what I use.


Edit: I ended up using C# from another box. It only took downloading a single assembly and about the same amount of code as given in the answers below.


回答1:


Something quick in PHP:

<?php
$connection = mysql_connect("mysqlserver.example.com", "username", "password");
mysql_select_db("dbname");
$sql = "SELECT `blob_column`, `id` FROM `mytable`";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
    file_put_contents("file" + $row["id"] + ".dat", $row["blob_column"]);
}
mysql_close($connection);

You could probably do something simular with whatever method you have to access MySQL, but AFAIK, there's no way to do it with pure SQL.




回答2:


In Common Lisp, using CLSQL, something like the following should work (untested, don't have MySQL installed at the moment):

(require 'clsql)
(require 'clsql-mysql)

(clsql:connect (host db user password port) :database-type :mysql)

(clsql:do-query ((col1 col2 blob) "select col1,col2,blob from blobtable")
  (with-open-file (outfile (format nil "~a-~a" col1 col2)
                           :direction :output
                           :element-type 'byte)
    (write-sequence blob outfile)))

You would have to fill in host, db etc. (port is optional), and adjust the query, of course.



来源:https://stackoverflow.com/questions/1051398/dumping-blob-rows-to-files-from-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!