Solved

TQuery and SQL String

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now