MySQL 如何在MySQL数据库中插入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5959043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert a file in MySQL database?
提问by meetpd
I want to insert a file in MYSQL database residing on a remote webserver using a webservice.
我想使用网络服务在驻留在远程网络服务器上的 MYSQL 数据库中插入一个文件。
My question is: What type of table column (e.g. varchar, etc.) will store a file? And will the insert statement be somewhat different in case of a file?
我的问题是:什么类型的表列(例如 varchar 等)将存储文件?如果是文件,插入语句会有所不同吗?
采纳答案by Brian Webster
The BLOB datatype is best for storing files.
BLOB 数据类型最适合存储文件。
- See: How to store .pdf files into MySQL as BLOBs using PHP?
- The MySQL BLOB reference manualhas some interesting comments
- 请参阅:如何使用 PHP 将 .pdf 文件作为 BLOB 存储到 MySQL 中?
- MySQL BLOB 参考手册有一些有趣的评论
回答by Lukas Liesis
File size by MySQL type:
MySQL 类型的文件大小:
- TINYBLOB255 bytes = 0.000255 Mb
- BLOB65535 bytes = 0.0655 Mb
- MEDIUMBLOB16777215 bytes = 16.78 Mb
- LONGBLOB4294967295 bytes = 4294.97 Mb= 4.295 Gb
- TINYBLOB255 字节 = 0.000255 Mb
- BLOB65535 字节 = 0.0655 Mb
- MEDIUMBLOB16777215 字节 = 16.78 Mb
- LONGBLOB4294967295 字节 = 4294.97 Mb= 4.295 Gb
回答by David Steele
The other answers will give you a good idea how to accomplish what you have asked for....
其他答案将使您对如何完成您所要求的事情有一个很好的了解....
However
然而
There are not many cases where this is a good idea. It is usually better to store only the filename in the database and the file on the file system.
在很多情况下,这是一个好主意。通常最好只存储数据库中的文件名和文件系统中的文件。
That way your database is much smaller, can be transported around easier and more importantly is quicker to backup / restore.
这样您的数据库就会小得多,可以更轻松地传输,更重要的是可以更快地备份/恢复。
回答by Daniel Bang
You need to use BLOB, there's TINY, MEDIUM, LONG, and just BLOB, as with other types, choose one according to your size needs.
你需要使用BLOB,有TINY、MEDIUM、LONG,只有BLOB,和其他类型一样,根据你的尺寸需求选择一种。
TINYBLOB 255
BLOB 65535
MEDIUMBLOB 16777215
LONGBLOB 4294967295
(in bytes)
The insert statement would be fairly normal. You need to read the file using freadand then addslashesto it.
插入语句是相当正常的。您需要使用fread然后读取文件addslashes。

