Lazarus
Programming => Databases => Topic started by: Lloydie-T 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?
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.
-
Just a guess: try to add zeoslib as a dependency to your project. (project->project inspector)
-
Did that. Now i get a new error.
Project raised exception class "EZDatabaseError" with message: SQL Query is empty
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.
-
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)
-
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?
-
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.
-
Thanks. I'll do that. ;)