I’m baffled by what's going on here. It's 0542 and I'm exhausted, so it’s probably something stupid.
I’ve written a super-simple wrapper around SQLite. In it, I have methods to convert UUID
to string, and bind it to columns as text. Here’s the thing: it always changes my UUID strings to "187178B5-C411-49E5-AE81-6D685A6C8B33"
, but text literals turn out okay:
try self.db.prepare(sql: "insert into Measurement (id, timestamp, sensor, temperature, furnaceID, experimentID) values(?, ?, ?, ?, ?, ?);")
try self.db.bind(idx: 1, val: UUID())
try self.db.bind(idx: 2, val: Date())
try self.db.bind(idx: 3, val: "sample")
try self.db.bind(idx: 4, val: Float.random(in: 900.0...2000.0))
try self.db.bind(idx: 5, val: UUID(uuidString: "FBD8EBC2-8806-4129-BEB6-8B7B1D5A760B")!)
try self.db.bind(idx: 6, val: UUID(uuidString: "187178B5-C411-49E5-AE81-6D685A6C8B33")!)
Swift.print("Stmt: \(self.db.stmtString)")
try self.db.step()
I have some Swift.print()
statements in my wrapper, and executing the code above on an empty table, it prints out:
Binding idx 1 to text val [5237AD9B-FFDF-4BFD-8B8A-676C2846D094]
Binding idx 2 to double val [631115696.888909]
Binding idx 3 to text val [sample]
Binding idx 4 to double val [1185.1787109375]
Binding idx 5 to text val [FBD8EBC2-8806-4129-BEB6-8B7B1D5A760B]
Binding idx 6 to text val [187178B5-C411-49E5-AE81-6D685A6C8B33]
Stmt: Optional("insert into Measurement (id, timestamp, sensor, temperature, furnaceID, experimentID) values(\'187178B5-C411-49E5-AE81-6D685A6C8B33\', 631115696.888909, \'sample\', 1185.1787109375, \'187178B5-C411-49E5-AE81-6D685A6C8B33\', \'187178B5-C411-49E5-AE81-6D685A6C8B33\');")
Note the UUID values that get printed, and then how they look in the expanded SQL! But "sample"
is unmodified. Both use the same code path.
If I swap the binding calls for value indexes 3 & 6 (as well as the column names in the prepare statement), then the UUID values are all "FBD8EBC2-8806-4129-BEB6-8B7B1D5A760B"
, and "sample"
becomes the corrupted "8z?l?"
when examined in the sqlite3
command line tool.
I’m just…baffled.