When connecting to Oracle DB I get External: SIGSEGV error

Wario

I'll say how I reproduce the problem on lazarus. I have a form and a datamodule using zeos to enstablish a connection with a local oracle db. The problem born when I put some code to interlocute with the db. Here is an example:

OracleMng.ZQuery1.SQL.Clear;      

That is exactly the line going in error. Here is the full code of the form:

 unit form1;

    {$mode objfpc}{$H+}
    
    interface
    
    uses
      Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBGrids, StdCtrls,
      datamodule2;
    
    type
    
      { TLogin }
    
      TLogin = class(TForm)
        Button1: TButton;
        DBGrid1: TDBGrid;
        procedure Button1Click(Sender: TObject);
      private
    
      public
    
      end;
    
    var
      Login: TLogin;
    
    implementation
    
    {$R *.lfm}
    
    { TLogin }
    
    procedure TLogin.Button1Click(Sender: TObject);
    begin
    
      OracleMng.ZQuery1.SQL.Clear;
    
    end;
    
    end.

Here is the code of the datamodule:

unit datamodule2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DB, ZConnection, ZDataset, ZSqlMonitor;

type

  { TOracleMng }

  TOracleMng = class(TDataModule)
    DataSource1: TDataSource;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
  private

  public

  end;

var
  OracleMng: TOracleMng;

implementation

{$R *.lfm}

{ TOracleMng }



end.

I'm trying

    if (OracleMng <>  Nil) and (OracleMng.Zquery1 <> Nil) then OracleMng.ZQuery1.SQL.add('select * from help');
  if (OracleMng <>  Nil) and (OracleMng.Zquery1 <> Nil) then OracleMng.ZQuery1.ExecSQL;
   dbgrid1.refresh; 

I have no more errors but the DBGrid1 is not filled.

This is my project lpr file:

program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, zcomponent, datamodule2, form1
  { you can add units after this };

{$R *.res}

begin
  RequireDerivedFormResource:=True;
  Application.Scaled:=True;
  Application.Initialize;
  Application.CreateForm(TLogin, Login);
  Application.Run;
end.        
MartynA

The fact that the change I suggested in my comment, namely

if (OracleMng <>  Nil) and (OracleMng.Zquery1 <> Nil) then
  OracleMng.ZQuery1.SQL.Clear

evidently stopped you getting the SIGSEGV error suggests that your DataModule and form are being created in the wrong order, i.e. form first. Check this out by going to Project | View Source in the IDE. If you see something like

program MyProgram;

[...]

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TDataModule1, DataModule1);
  Application.Run;
end.

they are in the wrong order, so swap the two CreateForm lines

  Application.CreateForm(TDataModule1, DataModule1);
  Application.CreateForm(TForm1, Form1);

With that change, you should no longer need the

if (OracleMng <>  Nil) and (OracleMng.Zquery1 <> Nil) then`

Next thing: You seem to be confused about when to use

ZQuery1.ExecSQL

and

ZQuery1.Open

Open is intended for when the SQL statement you are using produces a result set, that is a collection of records which can be viewed in a TDBGrid. The most usual way to do this is to use a SELECT statement as in

ZQuery1.SQL.Text := 'select * from MyTable';
ZQuery1.Open;

ExecQuery is intended for use where your SQL statement performs some operation on the database which does not involve SELECTing records. The most common SQL statements which need ExecSQL are

UPDATE
INSERT
DELETE

though there are others, for example statements which execute stored procedures on the SQL Server (note that some stored procedures return result sets and so need Open, rather than ExecSQL).

Note that ExecSQL will clear out any records which are in the dataset (ZQuery1) so after you need to do Open again using a suitable SQL statement

var
  S : String;
begin
  S := 'update MyTable set number = number +1 where id = 5';
  ZQuery.SQL.Text := S;
  ZQuery1.ExecSQL;    // no records shown in DBGrid1 from here

  S := 'select * from MyTable';
  ZQuery.SQL.Text := S;
  ZQuery1.Open;    // records shown in DBGrid1 again
end;

Note that I do

  S := 'select * from MyTable';
  ZQuery.SQL.Text := S;

instead of

  ZQuery1.SQL.Clear;
  ZQuery1.SQL.Add('select * from myTable');

The reason for this is that it's much easier to see the whole SQL statement in the debugger by inspecting the variable S than inspecting the ZQuery1.SQL.Text property and much easier to see any syntax errors.

You should always Close a dataset that you've Opened once you have finished working with it as it ensures what the data on disk is up to date. if the last SQL operation was ExecSQL, you don't need to close the dataset.

If you set the query's Text property the way I do, with ZQuery1.SQL.Text, you don't need to uses Clear. In any case, it is only equivalent to doing ZQuery1.SQL.Text := '' and it does not affect the state of the dataset - it only does anything when you call ExecSQL or Open.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I get a nullpointerexception when connecting to oracle DataBase

When connecting to Grafana I get an error 502

Get error code when connecting to Postgres DB fail

Error when connecting to external db - mysqli_connect(): (HY000/2002): Connection timed out

Errors when connecting oracle DB with Power BI

Why i get the connection string error when connect the oracle DB with c#?

I get a status 200 when connecting to the websocket, but it is an error?

Why do I get SIGSEGV error when use class function from pointer?

Connecting to Oracle DB in Go

How to use EXTRACT through dbplyr when connecting to an Oracle DB

Java Socket Timesout when I try connecting to server with external IP

mySql error connecting DB

Lazarus error "External: SIGSEGV" on variable increment?

Kerberos error when connecting to Oracle database in .NET Core

I get StackOverflow error when I create new instance in external class

Google Cloud Mongo DB: External IP not connecting

get error, when add external dependency in maven

Pivotal Cloud Foundry - Connecting to external oracle database

codeigniter 3 error connecting to db

Error connecting to local db server

Error while connecting to DB with hibernate

What does the "dmlreadexception" error mean on Moodle live logs when I try to pull from an external db log? And any suggestions to fix?

when i run "npm run dist_win", get a error: "node_modules" Not an internal or external command?

I get this error when i run heroku run rake db:migrate in production

I get this error when I insert a new post in the db "Call to a member function load() on null"

When I put PDO db connect details in an include file I get an error

Connecting to Oracle DB from IIS. Error: The underlying provider failed on Open

Error when connecting to database

Db Error while connecting with Yii Db settings