testpg1.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Program testpg;
  2. { Demo program to test pascal connection with postgres database }
  3. { Translated from the testlibpq example program of PostGreSQL }
  4. Uses postgres,strings;
  5. Procedure exit_nicely(Conn : PPGconn);
  6. begin
  7. PQfinish(conn);
  8. halt(1);
  9. end;
  10. Var
  11. pghost,pgport,pgoptions,pgtty,dbname : Pchar;
  12. nFields,i,j : longint;
  13. conn : PPGConn;
  14. res : PPGresult;
  15. begin
  16. pghost := NiL; { host name of the backend server }
  17. pgport := NiL; { port of the backend server }
  18. pgoptions := NiL; { special options to start up the backend server }
  19. pgtty := NiL; { debugging tty for the backend server }
  20. dbName := 'template1';
  21. { make a connection to the database }
  22. conn := PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
  23. { check to see that the backend connection was successfully made }
  24. if (PQstatus(conn) = CONNECTION_BAD) then
  25. begin
  26. Writeln (stderr, 'Connection to database ',dbname,' failed.');
  27. Writeln (stderr, PQerrorMessage(conn));
  28. exit_nicely(conn);
  29. end;
  30. { start a transaction block }
  31. res := PQexec(conn, 'BEGIN');
  32. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  33. begin
  34. Writeln (stderr, 'BEGIN command failed');
  35. PQclear(res);
  36. exit_nicely(conn);
  37. end;
  38. {
  39. * should PQclear PGresult whenever it is no longer needed to avoid
  40. * memory leaks
  41. }
  42. PQclear(res);
  43. {
  44. * fetch instances from the pg_database, the system catalog of
  45. * databases
  46. }
  47. res := PQexec(conn, 'DECLARE myportal CURSOR FOR select * from pg_database');
  48. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  49. begin
  50. Writeln (stderr, 'DECLARE CURSOR command failed');
  51. PQclear(res);
  52. exit_nicely(conn);
  53. end;
  54. PQclear(res);
  55. res := PQexec(conn, 'FETCH ALL in myportal');
  56. if (PQresultStatus(res) <> PGRES_TUPLES_OK) then
  57. begin
  58. Writeln (stderr, 'FETCH ALL command didn''t return tuples properly');
  59. PQclear(res);
  60. exit_nicely(conn);
  61. end;
  62. { first, print out the attribute names }
  63. nFields := PQnfields(res);
  64. for i := 0 to nFields-1 do
  65. Write (PQfname(res, i),space (15-strlen(PQfname(res, i))) );
  66. writeln;
  67. writeln;
  68. { next, print out the instances }
  69. for i := 0 to PQntuples(res)-1 do
  70. begin
  71. for j := 0 to nFields-1 do
  72. write(PQgetvalue(res, i, j),space (15-strlen(PQgetvalue(res, i,j))));
  73. writeln;
  74. end;
  75. PQclear(res);
  76. { close the portal }
  77. res := PQexec(conn, 'CLOSE myportal');
  78. PQclear(res);
  79. { end the transaction }
  80. res := PQexec(conn, 'END');
  81. PQclear(res);
  82. { close the connection to the database and cleanup }
  83. PQfinish(conn);
  84. end.