Page 1 of 1

" & quote...

Posted: Wed Aug 07, 2019 11:46 pm
by GuidoSarducci
So, I'm working with what should be a simple piece of code - it strips data from a temporary holding space and cleans it for insertion into a database. Or not:

-- Prep for Date
repeat until line 1 of tData contains <td class="date">
delete line 1 of tData
end repeat

I haven't figured out how to "properly" escape those quotes, regardless of research and forum information. Of course, it appears that part of my problem may be the keyword "date" combined with & quote options that causes the problem.

What, pray tell, am I doing wrong?

Re: " & quote...

Posted: Wed Aug 07, 2019 11:59 pm
by SparkOut

Code: Select all

repeat until line 1 of tData contains "<td class=" & quote & "date" & quote & ">"
   delete line 1 of tData
   wait 0 milliseconds with messages
end repeat
should do the trick

Re: " & quote...

Posted: Thu Aug 08, 2019 12:07 am
by GuidoSarducci
Good one - solved the problem!

Re: " & quote...

Posted: Thu Aug 08, 2019 8:24 am
by SparkOut
A more robust solution, and a handy technique for you:
1) a function can make the quote handling easier

Code: Select all

function repQ pText,pQ
   replace pQ with quote in pText
   return pText
end repQ
You can do your typing and swap another character for a double quote to make it easier, in the example below, use ~ as if you were typing a double quote inside the literal string. The second parameter tells the function which character you used to be the fake quote

Code: Select all

   put repQ("<td class=~date~>","~") into tSearch
   repeat while tSearch is not in line 1 of tData and tData is not empty
      delete line 1 of tData
      wait 0 milliseconds with messages
   end repeat
2) The code as you had it would loop forever if the data did not contain the target string. This example will end the repeat if the data becomes empty. (Which would probably be just as bad for your program operation but still.)
The wait with messages is a line which gives the engine a moment of as near zero duration as possible but stops the repeat loop becoming blocking and your application unresponsive if there is a lot of data to chunk through.

Re: " & quote...

Posted: Thu Aug 08, 2019 2:03 pm
by dunbarx
Hypercard introduced the "quote" as a constant over 2000 years ago just for this purpose, that the syntactical requirement for quoted strings made it necessary to have a, er, quote available "outside" and apart from such strings.

"Tab" is a similar constant, but is not so intimately embedded into the syntactic structure of our beloved xTalk paradigm, and can be far more easily differentiated between the constant itself and the character created with a keypress.

Craig

Re: " & quote...

Posted: Thu Aug 08, 2019 4:09 pm
by cpuandnet
If the end result is just to escape the quotes so you can insert it into a database, i find it would just be easier to use query placeholders and let LiveCode work it's magic. And it is safer since it would avoid SQL injections.

Code: Select all

...
put "INSERT INTO html_table (html_field) VALUES (:1)" into tQuery
revExecuteSQL tDatabaseID, tQuery, "tData"
...