Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
SQLite BLOB literals#
I wanted to construct a string of SQL that would return a blob value:
1select 'binary-data' as content, 'x.jpg' as content_filenameThis was while writing a unit test for datasette-media - for issue #19. I used it in the test here.
The SQLite documentation for Literal values explains how to do this:
BLOB literals are string literals containing hexadecimal data and preceded by a single “x” or “X” character. Example: X’53514C697465’
In Python 3 you can generate the hexadecimal representation of any byte string using b'...'.hex()
So my solution looked like this:
1jpeg_bytes = open("content.jpg", "rb").read()2sql = "select X'{}' as content, 'x.jpg' as content_filename".format(jpeg_bytes.hex())Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.