Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
What's the better way to store base64 in SQLite Database?
What's the better way to store base64 in SQLite Database? I was thinking to put base64 as TEXT since base64 has lots of chars.
CREATE TABLE name (id INTEGER PRIMARY KEY AUTO_INCREMENT, image TEXT);
Is there any other value in SQL for base64? I don't want to use bitmap (BLOB)[1].
-
not removing the whole text since someone had mentioned it in answer. And I forgot the name that's why I used bitmap earlier. ↩︎
1 answer
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
Anonymous | (no comment) | Oct 8, 2021 at 04:33 |
The question seems to show a certain level of confusion about terminology. I shall address what I believe to be the underlying question: How can I store binary data in SQLite?
SQLite supports 5 datatypes: NULL, INTEGER, REAL, TEXT, and BLOB. The natural datatype to use for binary data is BLOB. However, I assume that this is what you mean by bitmap when you say "I don't want to use bitmap".
That leaves text encodings. Note that
The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE)
so the most efficient text encoding to use depends on the database configuration.
If the database is using UTF-8 then the optimal encoding would be base-128 using codepoints \u0000 to \u007f. If for some reason you want to avoid certain codepoints, binary base encoding is fastest when it can work with blocks, i.e. with bases whose base-2 logarithm is just more than a fraction whose numerator is a small multiple of 8. E.g. if lg b = (8n + epsilon)/d
you can encode a block of n
bytes as a block of d
bytes for efficiency n/d
. That gives the following good bases:
Base b lg b Efficiency
41 16.03 / 3 0.67
49 56.15 / 10 0.70
53 40.10 / 7 0.71
57 64.16 / 11 0.72
64 24.00 / 4 0.75
75 56.06 / 9 0.78
85 32.05 / 5 0.80
102 40.03 / 6 0.83
116 48.01 / 7 0.86
128 56.00 / 8 0.88
So basically you want to pick the highest base which gives you enough codepoints in the range \u0000 to \u007f which you deem acceptable.
If the database is using UTF-16 then the basic principles are the same but you have the complication of working around the surrogate codepoints. I would avoid this if at all possible.
0 comment threads