1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
Program testpg;
{ Demo program to test pascal connection with postgres database }
{ Translated from the testlibpq example program of PostGreSQL }
Uses postgres,strings;
Procedure exit_nicely(Conn : PPGconn);
begin
PQfinish(conn);
halt(1);
end;
Var
pghost,pgport,pgoptions,pgtty,dbname : Pchar;
nFields,i : longint;
conn : PPGConn;
res : PPGresult;
dummy : string;
begin
pghost := NiL; { host name of the backend server }
pgport := NiL; { port of the backend server }
pgoptions := NiL; { special options to start up the backend server }
pgtty := NiL; { debugging tty for the backend server }
if paramcount=1 then
begin
dummy:=paramstr(1)+#0;
dbname:=@dummy[1];
end
else
dbName := 'testdb';
{ make a connection to the database }
conn := PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
{ check to see that the backend connection was successfully made }
if (PQstatus(conn) = CONNECTION_BAD) then
begin
Writeln (stderr, 'Connection to database ',dbname,' failed.');
Writeln (stderr, PQerrorMessage(conn));
exit_nicely(conn);
end;
res := PQexec(conn, 'select * from email');
if (PQresultStatus(res) <> PGRES_TUPLES_OK) then
begin
Writeln (stderr, 'select command failed.');
PQclear(res);
exit_nicely(conn);
end;
{ first, print out the attribute names }
nFields := PQnfields(res);
Write ('|',PQfname(res, 0),space (4-strlen(PQfname(res, 0))) );
Write ('|',PQfname(res, 1),space (20-strlen(PQfname(res, 1))) );
Write ('|',PQfname(res, 2),space (40-strlen(PQfname(res, 2))) );
writeln ('|');
writeln ('+----+--------------------+----------------------------------------+');
{ next, print out the instances }
for i := 0 to PQntuples(res)-1 do
begin
write('|',PQgetvalue(res, i, 0),space (4-strlen(PQgetvalue(res, i,0))));
write('|',PQgetvalue(res, i, 1),space (20-strlen(PQgetvalue(res, i,1))));
write('|',PQgetvalue(res, i, 2),space (40-strlen(PQgetvalue(res, i,2))));
writeln ('|');
end;
PQclear(res);
{ close the connection to the database and cleanup }
PQfinish(conn);
end. $Log: testpg2.pp,v $
end. Revision 1.3 2005/02/14 17:13:19 peter
end. * truncate log
end.
}
|