Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apostrophies in strings...

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi All

Having a bit of trouble saving strings to a database that contain apostrophies. The strings are read from a TEdit and a TMemo.

I'm writing the sql at runtime, eg
sql.add('Insert into Blah values ('''+Edit1.Caption+''')');

This fails with 'unterminated string' if the Caption has an apostrophy in it.

How can I get round this ?

I've also tried
sql.add('Insert into Blah values ("'+Edit1.Caption+'")');
but same problem.

I'm using Sybase.

lou
 
Also, later on I want to use said string in a POS function and I think I have the same problem (must check, DB down at the mo).

lou
 
In Pascal, single quotes in a string must be doubled. And you can do this using QuotedStr(). (Look it up in help.)

SQL, however, is different: you quote strings by adding a backslash before the single quote; e.g.
Code:
didn\'t
.

So just loop through the string, and if a character is
Code:
'
, then Insert() a
Code:
\
just before it. (That loop would be easiest if you go from the end of the string downto the beginning. That way, no need to worry about the fact that the string is getting longer as you go.)
 
Try use "Parametrized Query"

Query1.SQL.Add('Insert into Blah values :)pBlaValue)');
Query1.Prepare;
Query1.ParamByName('pBlaValue').AsString := Edit1.Text;
Query1.ExecSQL;
 
Thanks for that, rONIN441

...I could use the StringReplace fctn too.

big ta
lou X-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top