![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Подскажите как работать с libpq.dll для 8.0? [D7, Win2k, WinXP]
chili © (2005-06-01 14:47:00)Подскажите как работать с libpq.dll для 8.0?
делаю так:
type
PPGConn = ^PGConn;
PGConn = record
host : string;
hostaddr : string;
port : string;
dbname : string;
user : string;
password : string;
connect_timeout : string;
options : string;
tty : string;
sslmode : string;
requiressl : string;
service : string;
end;
function PQconnectdb(conninfo: PChar): PPGConn; stdcall; external 'libpq.dll'
chili (2005-06-02 12:47:00) [1]Большое спасибо
pasha_golub (2005-06-02 12:14:00) [2]Вообще-то, не полностью идентичный перевод. Это я уже сам игрался в последствии, но думаю ход мыслей ясен.
pasha_golub (2005-06-02 12:09:00) [3]Знач так. На форме лежит два Мемо. Один для данных, другой волучает извещения от сервера. Это стандартный пример из хелпа серверного, переведенный на Делфи.
Типы:
PGConn = pointer;
PgResult = pointer;
Фунции объявляются так:
TPQconnectdb = function(ConnInfo: PChar): PPGconn; cdecl;
var PQConnectdb: TPQconnectdb;
Соответственно подгрузка через LoadLibrary.
TPQstatus = function(Handle: PPGconn): ConnStatusType; cdecl;
TPQsetNoticeProcessor = function(Handle: PPGconn; Proc: PQnoticeProcessor; Arg: Pointer):pointer; cdecl;
TPQexec = function(Handle: PPGconn; Query: PChar): PPGresult; cdecl;
TPQresultStatus = function(Result: PPGresult): ExecStatusType; cdecl;
TPQclear = procedure(Result: PPGresult); cdecl;
TPQfinish = procedure(Handle: PPGconn); cdecl;
TPQerrorMessage = function(Handle: PPGconn): PChar; cdecl;
TPQntuples = function(Result: PPGresult): Integer; cdecl;
TPQgetvalue = function(Result: PPGresult; tup_num, field_num: Integer): PChar; cdecl;
Константы (выделены заглавными буквами) надо глядель в коде сервера, а именно в файле http://developer.postgresql.org/cvsweb.cgi/pgsql/src/interfaces/libpq/libpq-fe.h?rev=1.116;content-type=text%2Fplain
Желаю удачи. Но есть у меня сомнения в целесообразности такого низкого доступа к серверу в твоем случае.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, PSQLTypes, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure NoticeProcessor(arg: pointer; Msg: PChar); cdecl;
begin
Form1.Memo2.Lines.Append(Msg);
end;
procedure Main;
(*
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
} *)
(*int
main(int argc, char **argv)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
int nFields;
int i,
j;
*)
var
conninfo: PChar;
Conn: PGConn;
Res: PGResult;
i,j:integer;
nFields: integer;
s:string;
begin
(*
conninfo = "dbname = template1";
*)
Conninfo := 'host = ''localhost'' dbname = ''Test'' user = ''<user>'' password = ''<pass>''';
(*
/* Make a connection to the database */
conn = PQconnectdb(conninfo);
*)
Conn := PQConnectdb(ConnInfo);
(* /* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
*)
If PQStatus(Conn) <> CONNECTION_OK then
begin
ShowMessage('CAN''T START connection!');
PQFinish(Conn);
Exit;
end;
PQSetNoticeProcessor(Conn,NoticeProcessor,nil);
(*
/*
* Our test case here involves using a cursor, for which we must be
* inside a transaction block. We could do the whole thing with a
* single PQexec() of "select * from pg_database", but that's too
* trivial to make a good example.
*/
/* Start a transaction block */
res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/*
* Should PQclear PGresult whenever it is no longer needed to avoid
* memory leaks
*/
PQclear(res);
*)
Res := PQExec(conn,'BEGIN');
if PQResultStatus(res) <> PGRES_COMMAND_OK then
begin
ShowMessage('CAN''T START TRANSACTION');
PQClear(res);
PQFinish(Conn);
Exit;
end
else
PQClear(res);
res := PQExec(conn,'ANALYZE VERBOSE <tablename>');
if PQResultStatus(res) <> PGRES_TUPLES_OK then
begin
s := PQerrorMessage(conn);
ShowMessage('CAN''T START EXECUTING: '+S);
PQClear(res);
PQFinish(Conn);
Exit;
end;
(* /* first, print out the attribute names */
nFields = PQnfields(res);
for (i = 0; i < nFields; i++)
printf("%-15s", PQfname(res, i));
printf("\n\n");
*)
nFields := PQnFields(res);
for i:=0 to nFields-1 do
Form1.Memo1.Lines.Append(PQfname(res,i));
Form1.Memo1.Lines.Append('==============');
(*
/* next, print out the rows */
for (i = 0; i < PQntuples(res); i++)
{
for (j = 0; j < nFields; j++)
printf("%-15s", PQgetvalue(res, i, j));
printf("\n");
}
PQclear(res);
*)
for i:=0 to PQntuples(res)-1 do
begin
for j:=0 to nFields -1 do
Form1.Memo1.Lines.Append(PQgetvalue(res,i,j));
Form1.Memo1.Lines.Append('--------------');
end;
Form1.Memo1.Lines.Append('==============');
pqclear(res);
res := PQExec(conn,'END');
PQClear(res);
(*
/* close the connection to the database and cleanup */
PQfinish(conn);
return 0;
} *)
PQFinish(Conn);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Main;
end;
end.
chili (2005-06-02 10:45:00) [4]> pasha_golub
//Что именно нужно?
Ну если можно тогда примерчик как работать с Libpq.dll
pasha_golub (2005-06-01 18:00:00) [5]PGconn encapsulates a connection to the backend.
The contents of this struct are not supposed to be known to
applications.
pasha_golub (2005-06-01 17:57:00) [6]PPGConn = pointer;
И вообще, абсолютно все не то...
Что именно нужно?
pasha_golub (2005-06-01 17:52:00) [7]function PQconnectdb ..; cdecl;
Плохиш (2005-06-01 15:28:00) [8]Используя dbExpress
chili (2005-06-01 15:16:00) [9]help,help ну очень нужно