Quickie - replace all occurrences of '(apostrophe) with' '(two apostrophes) in AnsiString (C ++)

I think you can guess the problem I am having. I am inserting file names into sql database in C ++ Builder. Some files have an apostrophe in their name. This violates the sql insert request. the usual way to fix this is to double the apostrophes that you want to be part of the field value.

For example, if I want to add "george's" to the "owner" field, the sql query will "insert into table (owner) values" ("george 's") "

I'm fine with that. I just need to replace single apostrophes with double ones. For this, AnsiString does not have a built-in function. Is there an easy way to do this without having to include a whole new header file?

+5
source share
5 answers

Actually, I got the answer myself ...

item = StringReplace( item, "'", "''", TReplaceFlags() <<rfReplaceAll );

(therefore, an AnsiString has a built-in replacement function after it)

Edit: code tags have been added so we can distinguish between different quotes

+6
source

I did not use AnsiString, but basically I would do the following:

  • Reverse search for single quotes in your string
  • Look to the left and right of the current index of one quote
  • If there are no quotes, add one quote after your current index.
  • Continue the cycle until you press index 0.
+1
source

- , sql. SQL Injection , . , SQL-, - SQL-, . (. xkcd , .)

0

++ Builder AnsiQuotedStr SysUtils. AnsiExtractQuotedStr. AnsiString, .

, . , , , . , , .

, , , AnsiX UnicodeString, AnsiString, Delphi ++ Builder 2009.

0

,

item = StringReplace( item, "'", "\'", TReplaceFlags() );
0
source

All Articles