0

Is there a chance to edit the SQL code without erasing it? I Need the Query for much more tables and fields, and i can't add them all in the code it is more then 800 symbols

And this is much as i know about delphi whit sql:

  Form4.ADOQuery1.SQL.Clear;
  Form4.ADOQuery1.SQL.ADD('SELECT * FROM SKTech WHERE  SKtech.Tech='+#39+ComboBox1.Text+#39+';');
  Form4.ADOQuery1.Active := true;

As you can see i'm using sql whit combobox to chose different fields to show. This code works perfectly, but i can't add all my tables & fields... Is only way to do this is to store all the query in multiple strings?

4
  • ADO can return multiple recordsets by a single command execution (don't know if TADOQuery component though). However, the question is how comfortable will this be for you. Commented May 18, 2015 at 12:20
  • 1
    I don't understand what you're asking. SQL is simply text, so you can edit SQL code without erasing it all you want, in any way you want. I'd strongly suggest that you do a search on [delphi] sql parameters here, which will make your life much easier (and your code much more secure. Commented May 18, 2015 at 12:57
  • 2
    Do you know that you don't have to use the designtime components and that you can create your queries on the fly at runtime? And please do not use sql injection vulnerable queries and learn how to use parametrized queries. Commented May 18, 2015 at 13:14
  • 1
    Just small suggestion, instead of '+#39+ComboBox1.Text+#39+' use function QuotedStr() Commented May 18, 2015 at 19:38

1 Answer 1

1

For short queries I usually use a TStringList, that I can parse using a loop (or TStringList.IndexOf for a full string match) to locate "FROM", "WHERE" and other SQL tokens. Then I can insert new fields or conditions at the right place.

For longer queries (hundreds of lines) I sometimes use a TQueryBuilder class, based on the same principle but where query tokens are splitted:

TQueryBuilder = class
private
  FSelect: TStrings;
  FFrom: TStrings;
  FWhere: TStrings;
  FOrderBy: TStrings;
  ...
public
  function GetQuery: TStrings;

Then:

myDataset.SQL := myQueryBuilder.GetQuery;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.