Solved

TQuery and SQL String

Posted on 1998-12-01
2
166 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

689 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