How do I store binary data in a MySQL database?
Asked
Active
Viewed 1.8k times
3
-
Please update your question to include details of what you want to achieve (or what you tried and it failed). A simple search gives me how to store and retrieve binary data from a database. – verisimilitude Jun 11 '12 at 11:26
-
Please update your question. There is no details on what you are trying to do or what you have done so far. – Namphibian Jun 11 '12 at 12:48
-
why is this not a duplicate? – gloomy.penguin Dec 07 '13 at 01:10
3 Answers
14
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.
![](../../users/profiles/1100552.webp)
Eugen Rieck
- 60,102
- 9
- 66
- 89
6
I would suggest LONGBLOB
for storing files in MySQL
.
It all depends on what kind of binary and your application.
TINYBLOB - A BLOB column with a maximum length of 255 (2^8 - 1) characters.
BLOB - A BLOB column with a maximum length of 65,535 (2^16 - 1) characters.
MEDIUMBLOB - A BLOB column with a maximum length of 16,777,215 (2^24 - 1) characters.
LONGBLOB - A BLOB column with a maximum length of 4,294,967,295 (2^32 - 1) characters.
![](../../users/profiles/165071.webp)
Alexander Farber
- 18,345
- 68
- 208
- 375
![](../../users/profiles/692228.webp)
IEnumerable
- 3,230
- 11
- 42
- 72
2
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.
![](../../users/profiles/608170.webp)
verisimilitude
- 4,647
- 2
- 24
- 33