How to store binary data in MySQL [duplicate]

爱⌒轻易说出口 提交于 2019-12-19 10:18:10

问题


How do I store binary data in a MySQL database?


回答1:


This question is not so straight forward to answer, as it sounds: There are lots of different binary data usage patterns out there, each with their own caveats and pros and cons. Let me try to summarize:

  • Short pieces of binary data, such as password hashes, work very well by simply base64-encoding them and storing the resulting string as a VARCHAR
  • "Not-quite-binary" data, such as document snipplets with the occasional non-printable can be escaped and sored as a string
  • The BLOB datatype allows you to store arbitrary chunks of binary data, but I strongly recommend against using it: Store the data in a file, then store the path to the file in a String type. You gain nothing from storing binary data, that the DB doesn't "understand" in the DB.



回答2:


I would suggest LONGBLOB for storing files in MySQL. It all depends on what kind of binary and your appliction.

TINYBLOB - A BLOB column with a maximum length of 255 (28 - 1) characters.
BLOB - A BLOB column with a maximum length of 65,535 (216 - 1) characters.
MEDIUMBLOB - A BLOB column with a maximum length of 16,777,215 (224 - 1) characters.
LONGBLOB - A BLOB column with a maximum length of 4,294,967,295 (232 - 1) characters.



回答3:


Binary data can be stored in a MySQL database in a BLOB field. A BLOB is a binary large object that can hold a variable amount of data.



来源:https://stackoverflow.com/questions/10979100/how-to-store-binary-data-in-mysql

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