Solved

How to extract the SQL command?

Posted on 2001-06-29
9
402 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
ID: 6239255
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
ID: 6239364
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
ID: 6239380
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
ID: 6239413
//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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:ITugay
ID: 6240385
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
ID: 6240466
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
ID: 6240511
not very clear, what function do you mean?
0
 

Author Comment

by:leeboonseong
ID: 6249460
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
ID: 6250400
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

16 Experts available now in Live!

Get 1:1 Help Now