Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

71%
+3 −0
Q&A What's the better way to store base64 in SQLite Database?

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 da...

posted 2y ago by Peter Taylor‭

Answer
#1: Initial revision by user avatar Peter Taylor‭ · 2021-09-24T08:46:55Z (over 2 years ago)
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](https://www.sqlite.org/datatype3.html): 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.