19

There is a column type named blob in database, and it is used to store binary data.

But more often than not, I see solutions which compress binary data, then convert binary data to base64, and store base64 string as varchar or text in database.

Python code example:

import zlib, base64
base64_str = base64.b64encode(zlib.compress(binary_data, 9))

So there are two ways to store binary data into database:

  1. as blob
  2. as compressed base64

My Questions is: Which way is better and why?

Tyler Liu
  • 19,552
  • 11
  • 100
  • 84
  • 5
    I don't see why anyone would use method 2. It introduces needless complexity and obfuscates the data twice. However, I won't post this an an answer because I don't have any hard evidence that there are no cases where compressed base64 is beneficial over `BLOB`. – Polynomial Nov 21 '11 at 10:26

1 Answers1

22

It seems that I have to answer my own question. Most of the time, storing compressed base64 into database is not a good idea. It is more complex than storing blob. And most of the time binary is smaller than base64 string.

I only find one case that compressed base64 is useful: you can't alter the table schema, and there are only text columns, thus you have to store binary data into that table. The only possible way is to convert binary to base64 string.

Tyler Liu
  • 19,552
  • 11
  • 100
  • 84
  • 3
    Didn't know you could answer and accept your own question but well done! – Ashfaq Hussain Feb 18 '15 at 15:31
  • 1
    @AshfaqHussain, it is a very well accepted patter in SO (Stack Overflow). One can answer his own question and accept it as long it was a valid solution. – itsraghz Nov 27 '19 at 16:52