Recent

Author Topic: Zeoslib - Can't get simple query to work  (Read 12298 times)

Lloydie-T

  • New member
  • *
  • Posts: 9
Zeoslib - Can't get simple query to work
« on: February 02, 2009, 05:53:31 pm »
I am trying to create a console app which connects to a sqlite3 database. But I  seem to be doing something wrong. Any Ideas?
Code: [Select]
uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls,ZConnection, ZDataset ;


begin
     conn := TZConnection.Create(nil);
     query := TZQuery.Create(nil);
     query.Connection := conn;
       try
          try
             conn.Protocol := 'sqlite-3';
             conn.HostName := '';
             conn.Database := 'collex.sdb';
             conn.User := '';
             conn.Password := '';
             conn.Connect;
             query.Active:= true;
             query.SQL.Clear;
             query.SQL.Text := 'SELECT * from sys_val limit 0,1';
             query.Open;
             query.Last;
             CcLicense := query.FieldByName('cc_license').AsString
             finally
             query.Close;
             conn.Free;
         end;
        except
        on E: Exception do
        WriteLn(E.message);
     end;
end;

I get the error project1.lpr(1,1) Fatal: Can't find unit ZConnection used by Project1
even though I have installed the zeos package in lazarus.

Loesje

  • Full Member
  • ***
  • Posts: 165
    • Lazarus Support website
Re: Zeoslib - Can't get simple query to work
« Reply #1 on: February 02, 2009, 07:28:47 pm »
Just a guess: try to add zeoslib as a dependency to your project. (project->project inspector)

Lloydie-T

  • New member
  • *
  • Posts: 9
Re: Zeoslib - Can't get simple query to work
« Reply #2 on: February 02, 2009, 10:42:37 pm »
Did that. Now i get a new error.

Project raised exception class "EZDatabaseError" with message: SQL Query is  empty

Code: [Select]
program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, CustApp, ZConnection, ZDataset
  { you can add units after this };

type

  { TDbApp }

  TDbApp = class(TCustomApplication)
  protected
    procedure DoRun; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;

{ TDbApp }

procedure TDbApp.DoRun;
var
  ErrorMsg: String;
  conn :  TZConnection;
  query : TZQuery;
begin
  // quick check parameters
  ErrorMsg:=CheckOptions('h','help');
  if ErrorMsg<>'' then begin
    ShowException(Exception.Create(ErrorMsg));
    Halt;
  end;

  // parse parameters
  if HasOption('h','help') then begin
    WriteHelp;
    Halt;
  end;

  { add your program here }
    conn := TZConnection.Create(nil);
     query := TZQuery.Create(nil);
     query.Connection := conn;
       try
          try
             conn.Protocol := 'sqlite-3';
             conn.HostName := '';
             conn.Database := 'collex.sdb';
             conn.User := '';
             conn.Password := '';
             conn.Connect;
             query.Active:= true;
             query.SQL.Clear;
             query.SQL.Text := 'SELECT * from sys_val limit 0,1';
             query.Open;
             query.Last;
             writeln (query.FieldByName('cc_license').AsString)
             finally
             query.Close;
             conn.Free;
         end;
        except
        on E: Exception do
        WriteLn(E.message);
  // stop program loop
  end;
  Terminate;
end;

constructor TDbApp.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
end;

destructor TDbApp.Destroy;
begin
  inherited Destroy;
end;

procedure TDbApp.WriteHelp;
begin
  { add your help code here }
  writeln('Usage: ',ExeName,' -h');
end;

var
  Application: TDbApp;
begin
  Application:=TDbApp.Create(nil);
  Application.Title:='My DB App';
  Application.Run;
  Application.Free;
end.

Loesje

  • Full Member
  • ***
  • Posts: 165
    • Lazarus Support website
Re: Zeoslib - Can't get simple query to work
« Reply #3 on: February 02, 2009, 10:51:58 pm »
Another guess, I don't know ZEOS. But you set Query.Active := true, before you assigned a query. Remove that line. (Query.Active := True does the same as Query.Open)

Lloydie-T

  • New member
  • *
  • Posts: 9
Re: Zeoslib - Can't get simple query to work
« Reply #4 on: February 03, 2009, 02:14:06 am »
Thanks for that.
Weird this  is that if I run the app in the compiler, the  lines are not written to the console. But if I run the compiled app the lines are written. Is there a setting in lazarus stopping 'writeln' from working in the compiler?

Loesje

  • Full Member
  • ***
  • Posts: 165
    • Lazarus Support website
Re: Zeoslib - Can't get simple query to work
« Reply #5 on: February 03, 2009, 10:06:11 am »
You can not run programs in the compiler. The compiler creates an executable for your application, and that executable can be run.

You'd better start a new topic for this, it has nothing to do with databases. I can guess again: you are using Lazarus and run the program in the debugger. But then you have yourself to ask: where should it write it's output to... Is there a console-screen at all?!?

- But -

New topic.


Lloydie-T

  • New member
  • *
  • Posts: 9
Re: Zeoslib - Can't get simple query to work
« Reply #6 on: February 03, 2009, 01:49:03 pm »
Thanks. I'll do that. ;)

 

TinyPortal © 2005-2018