Conexión desde Delphi a PostgreSQL
He insertado en el form un componente TZMonitor, TZPgSqlDatabase, TZPgSqlTransact, TZPgSqlTable, TZPgSqlQuery y TZPgSqlNotify. No he tocado sus propiedades para nada porque creo que en el código se asigna dinámicamente todo lo necesario, sin embargo es mi primer acceso a base de datos.
Aunque compila bien, me da el siguiente error en la ejecución (la A llega a salir, la B no):
"Project PInformando.exe raised exception class EDatabaseError with message 'Database component not defined'."
Este es el código:
unit Informando;
interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, StdCtrls, ZConnect, ZPgSqlCon, ComCtrls, ZQuery, ZPgSqlQuery,
ZTransact, ZPgSqlTr;
type
TForm1 = class(TForm)
Nombre: TEdit;
ZPgSqlDatabase1: TZPgSqlDatabase;
StatusBar1: TStatusBar;
ZPgSqlTable1: TZPgSqlTable;
ZPgSqlQuery1: TZPgSqlQuery;
ZMonitor1: TZMonitor;
ZPgSqlTransact1: TZPgSqlTransact;
ZPgSqlNotify1: TZPgSqlNotify;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
procedure doConnect;
procedure Conectar;
procedure Desconectar;
public
{ Public declarations }
end;
var
Form1: TForm1;
function ExecSelectNColumnas (Query: TZPgSqlTable; SQL: string; numcol: integer; var SL: TStringList; var sterror: string): integer;
implementation
{$R *.DFM}
procedure TForm1.doConnect;
var sql, sterror: string;
aRecordCount: integer;
SLTemp: TStringList;
begin
SLTemp:= TStringList.Create;
Conectar;
if (ZPgSqlDatabase1.Connected) then
begin
sql:= 'SELECT * FROM noticias';
aRecordCount:= ExecSelectNColumnas (ZPgSqlTable1, sql, 1, SLTemp, sterror);
ShowMessage ('Nº Registros: '+ intToStr(aRecordCount) );
end;
Desconectar;
if Assigned (SLTemp) then SLTemp.Free;
end;
procedure TForm1.Conectar;
begin
try
ZPgSqlDatabase1.Connect;
except
on E: Exception do
begin
ShowMessage ('Error: ' + E.Message);
exit;
end;
end;
end;
procedure TForm1.Desconectar;
begin
try
ZPgSqlDatabase1.Disconnect;
StatusBar1.Panels[0].Text:= '';
except
on E: Exception do
begin
ShowMessage ('Error: ' + E.Message);
exit;
end;
end;
end;
function ExecSelectNColumnas (Query: TZPgSqlTable; SQL: string; numcol: integer; var SL: TStringList; var sterror: string): integer;
var i, aRecordCount: integer;
begin
SL.Clear;
sterror:= '';
try
Query.Sql.Text := Sql;
ShowMessage('A');
Query.Open;
ShowMessage('B');
aRecordCount:= Query.RecordCount;
ShowMessage('Nº Registros: '+ intToStr(aRecordCount));
Query.First;
while not Query.Eof do
begin
for i:= 0 to numcol - 1 do begin SL.Add(Query.Fields.AsString); end;
Query.Next;
end;
Query.Close;
result:= aRecordCount;
except
on E: Exception do
begin
sterror:= 'Error del Programa'+E.Message;
result:= -1;
exit;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ZPgSqlDatabase1.Host:= '10.0.0.3';
ZPgSqlDatabase1.Port:= IntToStr(5432);
ZPgSqlDatabase1.Database:= 'dbweb';
ZPgSqlDatabase1.LoginPrompt:= false;
ZPgSqlDatabase1.Login:= 'usuweb';
ZPgSqlDatabase1.Password:= 'sgenomicos';
end;//TForm1.FormCreate
procedure TForm1.FormShow(Sender: TObject);
begin
doConnect;
end;//TForm1.FormShow
end.
Aunque compila bien, me da el siguiente error en la ejecución (la A llega a salir, la B no):
"Project PInformando.exe raised exception class EDatabaseError with message 'Database component not defined'."
Este es el código:
unit Informando;
interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, StdCtrls, ZConnect, ZPgSqlCon, ComCtrls, ZQuery, ZPgSqlQuery,
ZTransact, ZPgSqlTr;
type
TForm1 = class(TForm)
Nombre: TEdit;
ZPgSqlDatabase1: TZPgSqlDatabase;
StatusBar1: TStatusBar;
ZPgSqlTable1: TZPgSqlTable;
ZPgSqlQuery1: TZPgSqlQuery;
ZMonitor1: TZMonitor;
ZPgSqlTransact1: TZPgSqlTransact;
ZPgSqlNotify1: TZPgSqlNotify;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
procedure doConnect;
procedure Conectar;
procedure Desconectar;
public
{ Public declarations }
end;
var
Form1: TForm1;
function ExecSelectNColumnas (Query: TZPgSqlTable; SQL: string; numcol: integer; var SL: TStringList; var sterror: string): integer;
implementation
{$R *.DFM}
procedure TForm1.doConnect;
var sql, sterror: string;
aRecordCount: integer;
SLTemp: TStringList;
begin
SLTemp:= TStringList.Create;
Conectar;
if (ZPgSqlDatabase1.Connected) then
begin
sql:= 'SELECT * FROM noticias';
aRecordCount:= ExecSelectNColumnas (ZPgSqlTable1, sql, 1, SLTemp, sterror);
ShowMessage ('Nº Registros: '+ intToStr(aRecordCount) );
end;
Desconectar;
if Assigned (SLTemp) then SLTemp.Free;
end;
procedure TForm1.Conectar;
begin
try
ZPgSqlDatabase1.Connect;
except
on E: Exception do
begin
ShowMessage ('Error: ' + E.Message);
exit;
end;
end;
end;
procedure TForm1.Desconectar;
begin
try
ZPgSqlDatabase1.Disconnect;
StatusBar1.Panels[0].Text:= '';
except
on E: Exception do
begin
ShowMessage ('Error: ' + E.Message);
exit;
end;
end;
end;
function ExecSelectNColumnas (Query: TZPgSqlTable; SQL: string; numcol: integer; var SL: TStringList; var sterror: string): integer;
var i, aRecordCount: integer;
begin
SL.Clear;
sterror:= '';
try
Query.Sql.Text := Sql;
ShowMessage('A');
Query.Open;
ShowMessage('B');
aRecordCount:= Query.RecordCount;
ShowMessage('Nº Registros: '+ intToStr(aRecordCount));
Query.First;
while not Query.Eof do
begin
for i:= 0 to numcol - 1 do begin SL.Add(Query.Fields.AsString); end;
Query.Next;
end;
Query.Close;
result:= aRecordCount;
except
on E: Exception do
begin
sterror:= 'Error del Programa'+E.Message;
result:= -1;
exit;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ZPgSqlDatabase1.Host:= '10.0.0.3';
ZPgSqlDatabase1.Port:= IntToStr(5432);
ZPgSqlDatabase1.Database:= 'dbweb';
ZPgSqlDatabase1.LoginPrompt:= false;
ZPgSqlDatabase1.Login:= 'usuweb';
ZPgSqlDatabase1.Password:= 'sgenomicos';
end;//TForm1.FormCreate
procedure TForm1.FormShow(Sender: TObject);
begin
doConnect;
end;//TForm1.FormShow
end.
1 Respuesta
Respuesta de markspiner