English 中文(简体)
如何在 MySQL 中插入 BLOB 和 CLOB 文件?
原标题:How to insert BLOB and CLOB files in MySQL?

我想用软件的前端存储图像和.docx/.doc、.pptx/.ppt、.pdf 文件。 我不明白如何执行此操作以及如何在表格中插入 BLOB 和 CLOB 文件。 请帮助 。

我使用Kubuntu 11.04, MySQL5, Qt 4.7.3。

问题回答

两种方式:

1 - 使用“http://dev.mysql.com/doc/refman/5.1/en/string-forests.html#formation_load-files”>LOAD_FILE 函数 -

INSERT INTO table1 VALUES(1, LOAD_FILE( data.png ));

- 将文件作为十六进制字符串插入文件,例如 -

INSERT INTO table1 VALUES 
  (1, x 89504E470D0A1A0A0000000D494844520000001000000010080200000090916836000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000001E49444154384F6350DAE843126220493550F1A80662426C349406472801006AC91F1040F796BD0000000049454E44AE426082 );
INSERT INTO MY_TABLE(id, blob_col) VALUES(1, LOAD_FILE( /full/path/to/file/myfile.png )

LOAD_FILE有许多附加条件。从MySQL文件:

LOAD_ FILE( 文件名称)

Reads the file and returns the file contents as a string. To use this function, the file must be located on the server host, you must specify the full path name to the file, and you must have the FILE privilege. The file must be readable by all and its size less than max_allowed_packet bytes. If the secure_file_priv system variable is set to a nonempty directory name, the file to be loaded must be located in that directory.

If the file does not exist or cannot be read because one of the preceding conditions is not satisfied, the function returns NULL.

此外,LOAD_FILE在Linux中也有虫子。见http://bugs.mysql.com/bug.php?id=38403 ,和 工作补差。在Ubuntu 12.04, MySQL 5.5.32,这对我有用:

  1. Copy file to /tmp
  2. Change ownership to mysql user chown mysql:mysql /tmp/yourfile
  3. Log into mysql as mysql root user so you are sure you have FILE privilege
  4. Run your insert statement
INSERT INTO table1 VALUES(1, LOAD_FILE(data.png));

工作但不工作,但

INSERT INTO table1 VALUES(1, LOAD_FILE( data.png ));

(假设当地目录中存在数据.png)

对于那些在通过查询保存 Blob 时获得“ Column 图像不可能是空的” 错误的人:-

打开您的 MySql 命令行客户端端端端口, 并用 root 用户和类型登录

mysql> SHOW VARIABLES LIKE "secure_file_priv";

这将显示 MySql 访问文件所使用的安全路径。 类似的东西

+------------------+-----------------------+
| Variable_name    | Value                 |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+

您可以在此文件夹中粘贴文件, 或者将“ security_ file_priv” 变量值更改为“ 空白字符串 ”, 这样它就可以从任何地方读取文件 。

如果您正在使用 Mysql 工作bench, 请在字段( 单元格) 上单击右键, 从文件选项中选择负载值, 然后浏览到文件, 单击打开并单击应用程序。 这将自动生成这样的查询 。

UPDATE `dbname`.`tablename` SET `columnname` = ? WHERE (`row` =  1 );




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签