Solved

How to extract the SQL command?

Posted on 2001-06-29
9
401 Views
Last Modified: 2011-10-03
Hi,
  I would like to ask how to extract the SQL command?
I got a program to create a file,in this program ppl can create a file with just select the fields that shown in the combo box.after selecting the fields then save the sql statement in this file.
  now I have to edit this file and extract the sql statement.let said the sql statement is: SELECT A,B,C FROM TIME. now i need to extract the A,B,C to the variable
a=A, b=B, c=C..so what I need to do?thans..for helping me.
0
Comment
Question by:leeboonseong
  • 3
  • 3
  • 2
  • +1
9 Comments
 

Author Comment

by:leeboonseong
Comment Utility
One more question..How can I pass the variable form form1 to form2? and also how to call fuction,procedure from form2 to form1?thanks
0
 
LVL 5

Expert Comment

by:knowlton
Comment Utility
As far as passing a var from one form to another:


Any variable declared in the INTERFACE portion of a unit is available to other units who use THAT unit.

So in Form1, interface section, declar a variable like this:

var
  Form1: TForm1;
  FormOneVar : string;  //Form 2 will access this

In Form2, you can click a button and access the contents of Form1, like this:

//OnClick for a button that fills an edit box with the Form1.FormOneVar:

//This is in Form2
procedure TForm2.Button1Click(Sender: TObject);
begin
  //Unit1 is the .pas file for Form1
  Edit1.Text := Unit1.FormOneVar
end;

Notice that Form2 (Unit2.pas)  USES Form1 (Unit1.pas):

...
...
...
var
  Form2: TForm2;

implementation

uses Unit1;  //Unit 2 uses Unit1

{$R *.DFM}

...
...
...





TO SEE THIS IN ACTION:

1) Open a new Project in Delphi with two forms (keep generic naming)
2)  On Form 1 drop a button.  The onclick for this button will open form 2.
3)  On Form 2 drop a TEdit and 2 buttons.  One button will close the form, the other will pull the variable from Form1 and display it's value in the TEdit on Form2.



//FORM 1 CODE:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ButtonOpenFormTwo: TButton;
    procedure FormCreate(Sender: TObject);
    procedure ButtonOpenFormTwoClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  FormOneVar : string;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FormOneVar := 'Hello from Form One';
end;

procedure TForm1.ButtonOpenFormTwoClick(Sender: TObject);
begin
  Form2 := TForm2.Create(Application);
  try
    Form2.ShowModal;
  finally
    Form2.Free;
  end;

end;

end.
 



//FORM 2 CODE

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm2 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
begin
  Edit1.Text := Unit1.FormOneVar
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
 Close;
end;

end.
0
 
LVL 4

Expert Comment

by:YodaMage
Comment Utility
If people have picked the fields, then basically do something like:

TQuery.SQL.Close;
TQuery.SQL := 'select * from ' + Choice1 + ',' + choice2 + (You get the idea, or you can pass the string value of the item index directly) + 'where (any conditions you need);
TQuery.ExecSQL;
TQuery.Open;

as far as the variable and procedure calls, have each form use the other. Then all you have to do is:

Variable from form 1 caled in form 2 as:

Form1.VarName

Procs and Functions in form 2 called in 1 as:

Form2.ProcName(Params);
0
 
LVL 5

Accepted Solution

by:
knowlton earned 50 total points
Comment Utility
//Quick and dirty

procedure ExtractVars(temp_SQL_statement : string);
var
 A, B, C : string;
 temp_str : string;
begin
  temp_str := temp_SQL_statement;
  //Delete phrase SELECT FROM from the SQL statement
  temp_str := Copy(temp_str, Pos('from', temp_str) + 4, 200);
  //Delete WHERE blah blah blah from the SQL_statement
  temp_str := Copy(temp_str, 1, Pos('where', temp_str));
  //Left with A, B, C now...
  A := Copy(temp_str, 1, Pos(',', temp_str));
  temp_str := Copy(temp_str, Pos(',', temp_str), 200);

  B := Copy(temp_str, 1, Pos(',', temp_str));
  temp_str := Copy(temp_str, Pos(',', temp_str), 200);

  C := Copy(temp_str, 1, 200);
  temp_str := Copy(temp_str, Pos(',', temp_str), 200);

end;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:ITugay
Comment Utility
Hi leeboonseong,

if your application is responsible for executed prepared by user file with SQL statement then you can do it by another way.

Do not save prepared SQL statement  to the file, save user selection line by line instead.

E.g.
user choosed table TABLENAME
and fields F1, F2, F3, F4
then file will looks something like this:
TABLENAME
F1
F2
F3
F4

Now you can easy prepare SQL statement for executing and do not need to parse statement because you know file structure, is it?

-----
Igor

PS: You can prepare, save and load file by using TStrinList. There is good properties Values.

List.Values['TABLE'] := 'TABLENAME';
List.Values['FIELD1'] := 'SOMEFIELD';

List.SaveToFile.....
List.LoadFromFile....
0
 

Author Comment

by:leeboonseong
Comment Utility
if the variable is in the form1..one procedure..can i use the same method to call this fuction..but where is place for me to declare the varible?
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
not very clear, what function do you mean?
0
 

Author Comment

by:leeboonseong
Comment Utility
thnaks for you help..can i have ur e-mail..so that next time i can send u my question?
0
 
LVL 5

Expert Comment

by:knowlton
Comment Utility
Thank you.

Tom Knowlton

knowltonfamily@yahoo.com

Send me any question you have, I will do my best to answer.

Experts-Exchange is also a GREAT resource for these types of questions.
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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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.

772 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

14 Experts available now in Live!

Get 1:1 Help Now