Hi All,
I am using Lazarus 1.0.10 with FPC 2.6.2 SVN Rev 41613 on Windows XP SP3.
I want to create an application that would run an SQL against an Oracle DB and spool the results in a file. Due to the fact that the SQL run is taking more than 10 minutes most of the time I want to run the SQL from a thread.
Below is the code that just runs a simple query.
The issue is that the code works while in the main thread but for some reason it stops when it gets to the SQLQuery.Open command.
No error message is provided, the code compiles with no issues.
Please advise what I am missing.
Thank you
unit Uthread_example;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, sqldb, oracleconnection, FileUtil, Forms,
Controls, Graphics, Dialogs, StdCtrls;
{ TMyThread }
Type
TMySQL = class(TThread)
private
parsedSQL:string;
procedure parseSQL;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: boolean);
end;
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
OracleConnection1: TOracleConnection;
OracleTransaction: TSQLTransaction;
SQLQuery: TSQLQuery;
ConnOracle: TOracleConnection;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
fs: TFileStream;
implementation
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
SQL: TMySQL;
begin
SQL := TMySQL.Create(True);
SQL.Start;
end;
{$R *.lfm}
procedure TMySQL.parseSQL;
begin
Form1.Memo1.Append(parsedSQL);
end;
procedure TMySQL.Execute;
begin
Form1.ConnOracle.UserName:= 'user';
Form1.ConnOracle.Password:= 'pwd';
Form1.ConnOracle.DatabaseName:= 'SID';
Form1.ConnOracle.HostName:= 'localhost';
Form1.ConnOracle.Close;
Form1.ConnOracle.KeepConnection:= True;
Form1.ConnOracle.Open;
Form1.OracleTransaction.DataBase:= Form1.ConnOracle;
Form1.SQLQuery.DataBase:= Form1.ConnOracle;
Form1.SQLQuery.Transaction:= Form1.OracleTransaction;
Form1.OracleTransaction.Active:= True;
Form1.SQLQuery.Close;
Form1.SQLQuery.SQL.Text:= 'select sysdate from dual';
Form1.SQLQuery.ExecSQL;
Form1.SQLQuery.Open; //does not seem to go beyong this point
parsedSQL := Form1.SQLQuery.Fields[0].AsString + '-' + FormatDateTime('YYYY-MM-DD HH-NN-SS',Now);
parsedSQL := FormatDateTime('YYYY-MM-DD HH-NN-SS',Now);
fs := TFileStream.Create('c:\ostemp\' + parsedSQL + '.txt', fmCreate);
//Synchronize(@parseSQL);
Form1.SQLQuery.Close;
Form1.ConnOracle.Close;
end;
constructor TMySQL.Create(CreateSuspended: boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
end.