Solved

TQuery and SQL String

Posted on 1998-12-01
2
165 Views
Last Modified: 2010-05-18
I'm using this query:

QueryTest.SQL.Add('Select * from TEST where Name = "' + TableNames['NAME'] + '"');

Ok. this works fine until the query reaches someone with the name "abcdf".

So what's happens is that the string looks like :
Select * from TEST where Name = ""abcd"" !!!

I also tried this :
QueryTest.SQL.Add('Select * from TEST where Name = ''' + TableNames['NAME'] + '''');

Replacing the " with ' works but when now someone has a name with '''' in it, the same problem occurs.

Is there a solution to my problem??

0
Comment
Question by:rick_mulder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1348757
hi rick_mulder

try

QueryTest.SQL.Add('Select * from TEST where Name = ' + quotedStr(TableNames['NAME'])');

meikl
0
 
LVL 3

Accepted Solution

by:
williams2 earned 50 total points
ID: 1348758
You should NOT begin to but extra quotes around the string, instead you should replace quotes ' with two quotes ''. It sounds a bit silly, but try it, it works ..also with multiple quotes '''' .

This should look like this

Username:= 'D''abo'; //This is the Delphi way to handle quotes and has nothing to do with the SQL-Statement.

var
  S,Username: String;
  i: Integer;
Begin
  S:= '';
  For i:= 1 to length(Username) do
  Begin
    S:= S+Username[i];
    if Username[i]='''' then S:= S + '''';
  End;

  QueryTest.SQL.Add('Select * from TEST where Name = '+QuotedStr(S)+');
End;

This works fine to me!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question