markdown 从SQL数据库导出多个blob /图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 从SQL数据库导出多个blob /图像相关的知识,希望对你有一定的参考价值。

# Exporting multiple blobs from an SQL database.

This is tested for MySQL with Linux or OSX

### 1. Get the IDs of the rows we want to export


```
select id from photo where home_id in (32,37,34,45,38,39,30,1,4,2,9,17)
```

```
+-----+
| id  |
+-----+
|  44 |
|  45 |
|  47 |
... 
```

I happy with the output then it can be used as a subquery for the next step.

### 2. Generate the SQL queries

...which will export binary blobs from these rows. One query per row. We modify the above query to output a series of queries instead of IDs.


```
select 
 CONCAT("select `original` into dumpfile '/tmp/dbDump/", `id`, ".jpg' from photo where `id` = ", `id`,';') as queries
 from photo where home_id in (32,37,34,45,38,39,30,1,4,2,9,17)
```

It might be better to add some meaningful information in the filename:


```
select 
 CONCAT(
 	"select `original` into dumpfile '/tmp/dbDump/",
 	home.label,
 	".", 
 	photo.id, 
 	".jpg' from photo where `id` = ", 
 	photo.id,
 	';') 
 as queries
 from photo left join home on photo.home_id = home.id 
 where home_id  in (32,37,34,45,38,39,30,1,4,2,9,17)
```

We would get:

```
+-----------------------------------------------------------------------------------+
| queries                                                                           |
+-----------------------------------------------------------------------------------+
| select `original` into dumpfile '/tmp/dbDump/Maison.44.jpg' from photo where `id` = 44   |
| select `original` into dumpfile '/tmp/dbDump/Maison.45.jpg' from photo where `id` = 45   |
| select `original` into dumpfile '/tmp/dbDump/Maison.47.jpg' from photo where `id` = 47   |
...
```


If using the command line mysql client, then the tabular output will contain some formatting pipe characters (`|`) as above, which need to be removed:

I used Sublime Text to run a regex replace, using the following regular expression:

`(^\| |\s\|$)`

...to find all formatting character and then deleted them

### 3. Save output

Make sure output directory exists and is writable

```
mkdir /tmp/dbDump
chmod 777 /tmp/dbDump
```

Now we just have to run these queries and pick up our blobs from `/tmp/dbDump`

The output can be zipped into a single file:

```
zip -3 -r photos.zip /tmp/dbDump
```

以上是关于markdown 从SQL数据库导出多个blob /图像的主要内容,如果未能解决你的问题,请参考以下文章

Azure sql 数据库导出到存储 blob 失败

Azure SQL 导出无法在 Blob 容器中备份

从global到mooncake迁移SQL Azure

Azure Sql 数据库导出到 Azure Blob 存储失败

将 SQL 查询结果作为 txt 文件自动导出到 Azure Blob 存储

SQL Server 导出 Blob 损坏所有文件