I am using sqlite3_exec()
function for executing the queries. I am aware that as per below post, it's better to use _prepare, _bind, _step, _finalize
combination for various benefits.
sqllite query with encrypted std::string (unrecognized token)
However, in my case I already have a library in place, which is tightly coupled around sqlite3_exec()
and it will be difficult to change. Even for education purpose, I would like to know, how can I enter special tokens inside an sqlite3 database. Currently I am programatically reading this statement from a file using C++ and inserting into the DB using sqlite3_exec()
Currently, following statement is giving error:
INSERT INTO Convey(username, data, id, type)
VALUES('12345', '^Z^Dfine* ^HºÉÙ<98>ö÷×^B2^@', 'abcd', 31);
The error comes at this symbol ^@
as SQL error: unrecognized token:
. When I dismantled the statement and checked ascii for each characters, it showed 0
in place of ^@
. The type of data
, be it TEXT
or BLOB
doesn't make a difference.
Note that, the "data", I am trying to enter here is a Google protobuf message in a serialised form.
What is the correct way to escape such "nul" like characters in sqlite, when one has to use sqlite3_exec()
?
[Note: 1 way is to replace 0 with some pre-defined pattern, which less likely to be found in binary strings and then restore them while reading it. I am open to this solution as well.]