?
Solved

TQuery and SQL String

Posted on 1998-12-01
2
Medium Priority
?
167 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 200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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