SQLite 3.50 in 2025: Reading and Reproducing Escaped Maintenance Labels
Decode a maintenance label with Unicode escapes. Local checks verify newlines and tabs, and show what happens to a SQL literal that encounters a NUL byte.
Archive retrospective · Topic year 2025
In this article
A device label can exist in two representations
Suppose a maintenance instrument exports a label with an escaped space between Raf and C. It uses another escape for a line break and one for a tab. A reader looking at the raw export can see the escape syntax. A maintenance console needs to show the label on two lines, with the temperature unit and the status intact. Receiving this value as a JavaScript string does not commit the database to interpreting it. The format used by the device determines when and whether escape sequences should be decoded. In this example the JavaScript string contains literal backslashes, and we ask SQLite to decode it once. SQLite’s May 29, 2025 release notes introduced unistr and unistr_quote in version 3.50.0. We use them in different directions and test which values survive a round trip.
Bind the input and decode once
The example prepares SELECT unistr(?) and binds its input through a parameter. String.raw keeps the backslashes in the JavaScript source visible to SQLite. With a label already read from a device, you would normally pass the acquired string directly. SQLite documents a lower-case u followed by four hexadecimal digits, an upper-case U followed by eight, and additional Unicode escape forms. The test uses U+0020 for a space, U+000A for the line break, U+00B0 for the degree sign, and U+0009 for the tab. These represent specific characters agreed in the input format. This function should not be substituted for a JSON parser. When preparing the fixture, the common backslash-n shortcut was rejected, so the final example supplies the Unicode form explicitly. Keep track of whether the decoder has already run when values go through multiple layers.
Inspect the decoded text and the generated literal
The in-memory database returns Raf C on the first line and 24°C followed by a tab and OK on the second. The application verifies that the line break and tab are actual characters in the returned value. It then asks unistr_quote to construct a SQL representation of that value. In SQLite 3.53.3, the output is an expression calling unistr, with the line break shown as the escape for 000a and the tab as 0009. The fixture evaluates that generated expression in a second SELECT to confirm that it produces the original decoded text. That step uses an expression generated by SQLite for a controlled test value. Ordinary application inputs still belong in parameters. A separate call supplied an invalid hexadecimal digit and confirmed rejection. The observed results use SQLite 3.53.3 from Node.js v24.19.0. The 2025 release notes establish when these functions were introduced.
import assert from 'node:assert/strict';
import { DatabaseSync } from 'node:sqlite';
const db = new DatabaseSync(':memory:');
const raw = String.raw`Raf\u0020C\u000A24\u00B0C\u0009OK`;
const input = db.prepare('SELECT unistr(?) AS value');
const display = input.get(raw).value;
const output = db.prepare('SELECT unistr_quote(?) AS literal').get(display);
const nul = db.prepare(String.raw`SELECT unistr_quote(unistr('A\u0000B')) AS literal`).get();
const hex = db.prepare(String.raw`SELECT hex(unistr('A\u0000B')) AS complete_bytes,
hex(${nul.literal}) AS quoted_bytes`).get();
assert.equal(display, 'Raf C\n24°C\tOK');
assert.equal(db.prepare(`SELECT ${output.literal} AS value`).get().value, display);
assert.deepEqual({...hex}, {complete_bytes:'410042',quoted_bytes:'41'});
const invalid = (() => {
try { input.get(String.raw`Raf\u12G4`); return 'accepted'; }
catch { return 'rejected'; }
})();
assert.equal(invalid,'rejected');
console.log(JSON.stringify({sqlite:db.prepare('SELECT sqlite_version() AS version').get().version,
display, literal:output.literal, bytesAfterNul:hex.complete_bytes,
quotedBytes:hex.quoted_bytes, malformed:invalid}));
db.close();
An embedded NUL is different
The test also asks SQLite to construct a value containing A, a zero byte and B. Passing that value directly to hex yields 410042. The middle byte is still present. After generating and re-evaluating a SQL representation with unistr_quote, the result is 41. In this case, the SQL representation stops at the embedded NUL. That is a different result from the one with only a new line and a tab. If the archive needs to reproduce device bytes exactly, confirm which fields should be treated as binary. A BLOB can preserve those bytes alongside a separate string for display. You can also choose to reject embedded NULs as invalid in text fields. Either approach makes the consequence explicit and testable. A report that looks readable cannot establish that bytes after a NUL survived an export.
Specify the boundaries of an import tool
An import has at least three format boundaries. The first is the format supplied by the device. State which escape grammar the decoder expects and which control codes are permitted in a text field. The second is the actual SQLite library at the destination. An application version does not prove that the bundled SQLite has a particular function, especially on a device with its own system library. The third boundary is the output target. A terminal, a browser view and a SQL dump may represent the same stored label in different ways. Their rules for escaping and display apply after storing or decoding the value. Separate tests can check what gets stored, what gets displayed and what an export can reproduce. The fixture here checks the database values directly in addition to showing serialized output to a human.
Make the data-loss case part of a regression run
Select one simple label and one real device sample with encoded control characters. Check the decoded value, reject a malformed escape and compare the bytes after exporting and rereading the sample. If a zero byte violates the text contract, report it at import with a useful reason rather than relying on a later export to expose the truncation. If that byte is legitimate, retain the raw data independently of a display value. This example uses a database in memory and short queries. A production recovery exercise should also replay the actual export format using the versions that will open the archive. Keeping a small, precise input fixture makes it easier to locate a change in decoding when a renderer or database library is upgraded.
Reader discussion
Which bottleneck would you expect as this solution scales?
No comments yet. Be the first to contribute.
Write a comment