SQLite handling quotes within quotes
lazarus / 16-Databases
----------------------------------------------------------------------
HatForCat · 2017-04-19 19:50
I have some incoming text that has quotations within it and SQLite is
seeing them as terminators for an Insert.
I load up a TStringList with incoming text then pass it to the
dm.In.SQL.Add(... function
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i {
...Incoming TStringList item #3
}
dm.In.SQL.Add('VALUES ( '); ... dm.In.SQL.Add(#34+strList[2]+#34+',
'); dm.In.SQL.Add(#34+strList[3]+#34+', '); ...
BUT one of the " for "ltr" is being seen as the closing quote --
crash!!
How do I handle that? Is there an escape character I should be using
with #34?
I have done some searching and not found anything to help, but it
might be my search terms
Thanks
----------------------------------------------------------------------
HeavyUser · 2017-04-19 20:07
1) always use the complete sql not parts
2) depending on the sql server you have to either escape the quote
there is the function quotedStr that does this for single quote <'>
strings (mssql,firebird etc) You can change the code for double quotes
or even better parameterize the quote char.
But I hear the mysql does not support double quoting by default you
need to set it.
----------------------------------------------------------------------
HatForCat · 2017-04-19 20:20
Thanks, I have found SQLite3 "QUOTE" which supposed to fix it but it
is not working or I am not using it correctly.
Also, I have found that using a single quote at each end would do it,
providing there are no single quotes embedded in the text.
This'll look "sweet!"
'This'll look "sweet!"' <-- Fail
"This'll look "sweet!"" <-- Fail
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
'QUOTE('+#34+strList[3]+#34+'), ';
----------------------------------------------------------------------
LacaK · 2017-04-20 05:31
If you want insert content of TStringList into any character column of
SQL table, then double quotes are not problem, but single quotes are.
As far as SQL requires that character values are enclosed in single
quotes and embedded single quote must be escaped by two single quotes.
You can use QuotedStr() function to handle this or better parameters
in SQL statement.
So try:
dm.In.SQL.Add(QuotedStr(strList[2])+', '); ... etc.
or:
dm.In.SQL.Add(':value, '); ... ParamByName('value').AsString :=
strList[2];
----------------------------------------------------------------------
HatForCat · 2017-04-20 14:58
@LacaK:
@HeavyUser:
Thanks "QuotedStr" simplified it all and solved the problem.
----------------------------------------------------------------------
mgear · 2017-04-29 17:39
Probably is not the best idea all in all to to pass the variables
within the text of the query. There are argument placeholders there
and it's the much more strict and portable practice.
Of course QuotedStr() is ok just here but it cultivates the bad
programming habits, because, generally, database (I mean real
database, not SQLite ) is not obliged to accept the exact value of the
big or the complex object by text, so, it wouldn't we saved at all or
would be saved broken. The database is always possible to accept it as
the query parameter, though. So it is THE good habit to use the
parameters ALWAYS when you use the variables in the queries.
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
SQLQuery1.SQL.Text := 'insert into mytable values (?,?)';or
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
SQLQuery1.SQL.Text := 'insert into mytable values (:param1,:param2)';
and, later, pass the parameters by name or by order:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
SQLQuery1.ParamByName('param1').AsInteger :=
MyIntVariable;SQLQuery1.Params[1].AsString :=
MyStringVariable;SQLQuery1.ExecSQL;