이 문서는 지원되지 않는 버전의 PostgreSQL 용입니다.
당신은에 대해 같은 페이지를 볼 수 있습니다current버전 또는 위에 나열된 다른 지원되는 버전 중 하나입니다.
스포츠 토토 베트맨
이전 40 장. libpq 다음

샘플 프로그램

샘플 프로그램 1

/*
          * testLibpq.c
          * Postgres Frontend 라이브러리 인 Libpq의 C 버전을 테스트하십시오.
          *
          *
          */
         #include <stdio.h
         #include "libpq-fe.h"

         무효의
         exit_nicely (pgconn* conn)

           pqfinish (conn);
           출구 (1);

         기본()

           char *pghost, *pgport, *pgoptions, *pgtty;
           char* dbname;
           int nfields;
           int i, j;

         / * 파일 * 디버그; */

           pgconn* conn;
           pgresult* res;

           /* 백엔드 연결의 매개 변수를 설정하여 시작
              매개 변수가 null이면 시스템이 사용하려고합니다.
              환경 변수를 찾아 합리적인 기본값
              또는 하드 유용 상수를 사용하여 실패 */
           pghost = null;  / * 백엔드 서버의 호스트 이름 */
           pgport = null;  / * 백엔드 서버의 포트 */
           pgoptions = null; / * 백엔드 서버를 시작하는 특수 옵션 */
           pgtty = null;     / * 백엔드 서버의 TTY 디버깅 */
           dbname = "template1";

           / * 데이터베이스에 연결 */
           conn = pqsetdb (pghost, pgport, pgoptions, pgtty, dbname);

           / * 백엔드 연결이 성공적으로 만들어 졌는지 확인하십시오 */
           if (pqstatus (conn) == connection_bad) 
             fprintf (stderr, "데이터베이스 '%s'에 대한 연결 실패 .0, dbname);
             fprintf (stderr, "%s", pqerrormessage (conn));
             exit_nicely (conn);

         /* debug = fopen ( "/tmp/trace.out", "w");  */
         /* pqtrace (Conn, Debug);  */

           / * 거래 블록 시작 */

           RES = PQEXEC (Conn, "시작");
           if (pqresultstatus (res)! = pgres_command_ok) 
             fprintf (stderr, "시작 명령 실패);
             PQCLEAR (RES);
             exit_nicely (conn);

           /* pqclear pgresult가 더 이상 피할 필요가 없을 때마다
              메모리 누출 */
           PQCLEAR (RES);

           /* 데이터베이스의 시스템 카탈로그*/
           res = pqexec (conn, "pg_database에서 select *에서 myportal cursor를 선언합니다");
           if (pqresultstatus (res)! = pgres_command_ok) 
             fprintf (stderr, "선언 커서 명령 실패);
             PQCLEAR (RES);
             exit_nicely (conn);

           PQCLEAR (RES);

           RES = PQEXEC (Conn, "MyPortal에서 모두 가져 오기");
           if (pqresultstatus (res)! = pgres_tuples_ok) 
             fprintf (stderr, "모든 명령은 튜플을 올바르게 반환하지 않았다.);
             PQCLEAR (RES);
             exit_nicely (conn);

           / * 먼저 속성 이름을 인쇄 */
           nfields = pqnfields (res);
           for (i = 0; i <nfields; i ++) 
             printf ( "%-15s", pqfname (res, i));

           printf ( "0);

           / * 다음으로 인스턴스를 인쇄 */
           for (i = 0; i <pqntuples (res); i ++) 
             for (j = 0; j <nfields; j ++) 
               printf ( "%-15s", pqgetValue (res, i, j));

             printf ( "0);

           PQCLEAR (RES);

           / * 포털 닫기 */
           res = pqexec (Conn, "Close myportal");
           PQCLEAR (RES);

           / * 거래 종료 */
           res = pqexec (conn, "end");
           PQCLEAR (RES);

           / * 데이터베이스 연결을 닫고 정리 */
           pqfinish (conn);

         /* fclose (Debug); */

샘플 프로그램 2

/*
          * testlibpq2.c
          * 비동기식 알림 인터페이스 테스트
          *
            다음과 함께 데이터베이스를 채우십시오.

         테이블 TBL1 (I int4)을 작성합니다.

         테이블 tbl2 (i int4)를 만듭니다.

         tbl1에 삽입 된 것과 같이 규칙 r1을 만듭니다 [tbl2 값 (new.i)에 삽입; 알림 tbl2];

          * 그런 다음이 프로그램을 시작하십시오
          * 프로그램이 시작된 후

         tbl1 값 (10)에 삽입;

          *
          *
          */
         #include <stdio.h
         #include "libpq-fe.h"

         void exit_nicely (pgconn* conn)

           pqfinish (conn);
           출구 (1);

         기본()

           char *pghost, *pgport, *pgoptions, *pgtty;
           char* dbname;
           int nfields;
           int i, j;

           pgconn* conn;
           pgresult* res;
           pgnotify* 알림;

           /* 백엔드 연결의 매개 변수를 설정하여 시작
              매개 변수가 null이면 시스템이 사용하려고합니다.
              환경 변수를 찾아 합리적인 기본값
              또는 하드 유용 상수를 사용하여 실패 */
           pghost = null;  / * 백엔드 서버의 호스트 이름 */
           pgport = null;  / * 백엔드 서버의 포트 */
           pgoptions = null; / * 백엔드 서버를 시작하는 특수 옵션 */
           pgtty = null;     / * 백엔드 서버의 TTY 디버깅 */
           dbname = getenv ( "사용자"); /* 테스트 데이터베이스 이름으로 변경*/

           / * 데이터베이스에 연결 */
           conn = pqsetdb (pghost, pgport, pgoptions, pgtty, dbname);

           / * 백엔드 연결이 성공적으로 만들어 졌는지 확인하십시오 */
           if (pqstatus (conn) == connection_bad) 
             fprintf (stderr, "데이터베이스 '%s'에 대한 연결 실패 .0, dbname);
             fprintf (stderr, "%s", pqerrormessage (conn));
             exit_nicely (conn);

           res = pqexec (conn, "청취 tbl2");
           if (pqresultstatus (res)! = pgres_command_ok) 
             fprintf (stderr, "청취 명령 실패);
             PQCLEAR (RES);
             exit_nicely (conn);

           /* pqclear pgresult가 더 이상 피할 필요가 없을 때마다
              메모리 누출 */
           PQCLEAR (RES);

           (1) 
               /* Async 알림 쿼리의 결과로만 다시 나옵니다*/
               / * 빈 쿼리를 보낼 수 있습니다 */
               res = pqexec (conn, "");
         / * printf ( "res- status = %s0, pgresstatus [pqresultstatus (res)]; */
               / * 비동기 반환을 확인하십시오 */
               notify = pqnotifies (conn);
               if (알림) 
                fprintf (stderr,
                     "Backend Pid '%D'수신 0에서 '%s'의 Async 알림,
                     notify- relname, notify- be_pid);
                무료 (알림);
                부서지다;

               PQCLEAR (RES);

           / * 데이터베이스 연결을 닫고 정리 */
           pqfinish (conn);

샘플 프로그램 3

/*
          * testlibpq3.c
          * Postgres Frontend 라이브러리 인 Libpq의 C 버전을 테스트하십시오.
          * 이진 커서 인터페이스를 테스트합니다
          *
          *
          *
          다음을 수행하여 데이터베이스를 채우십시오.

         테이블 test1 (i int4, d float4, p 다각형)을 생성하고;

         test1 값 (1, 3.567, '(3.0, 4.0, 1.0, 2.0)':: 다각형에 삽입;

         test1 값 (2, 89.05, '(4.0, 3.0, 2.0, 1.0)':: 다각형에 삽입;

          예상 출력은 다음과 같습니다.

         튜플 0 : 얻었습니다
          i = (4 바이트) 1,
          d = (4 바이트) 3.567000,
          P = (4 바이트) 2 포인트 바운드 박스 = (HI = 3.000000/4.000000, LO = 1.000000,2.000000)
         튜플 1 : 얻었습니다
          i = (4 바이트) 2,
          D = (4 바이트) 89.050003,
          P = (4 바이트) 2 포인트 바운드 박스 = (HI = 4.000000/3.000000, LO = 2.000000,1.000000)

          *
          */
         #include <stdio.h
         #include "libpq-fe.h"
         #include "utils/geo-decls.h"/ * 다각형 유형 */

         void exit_nicely (pgconn* conn)

           pqfinish (conn);
           출구 (1);

         기본()

           char *pghost, *pgport, *pgoptions, *pgtty;
           char* dbname;
           int nfields;
           int i, j;
           int i_fnum, d_fnum, p_fnum;

           pgconn* conn;
           pgresult* res;

           /* 백엔드 연결의 매개 변수를 설정하여 시작
              매개 변수가 null이면 시스템이 사용하려고합니다.
              환경 변수를 찾아 합리적인 기본값
              또는 하드 유용 상수를 사용하여 실패 */
           pghost = null;  / * 백엔드 서버의 호스트 이름 */
           pgport = null;  / * 백엔드 서버의 포트 */
           pgoptions = null; / * 백엔드 서버를 시작하는 특수 옵션 */
           pgtty = null;     / * 백엔드 서버의 TTY 디버깅 */

           dbname = getenv ( "사용자");  /* 테스트 데이터베이스 이름으로 변경*/

           / * 데이터베이스에 연결 */
           conn = pqsetdb (pghost, pgport, pgoptions, pgtty, dbname);

           / * 백엔드 연결이 성공적으로 만들어 졌는지 확인하십시오 */
           if (pqstatus (conn) == connection_bad) 
             fprintf (stderr, "데이터베이스 '%s'에 대한 연결 실패 .0, dbname);
             fprintf (stderr, "%s", pqerrormessage (conn));
             exit_nicely (conn);

           / * 거래 블록 시작 */
           RES = PQEXEC (Conn, "시작");
           if (pqresultstatus (res)! = pgres_command_ok) 
             fprintf (stderr, "시작 명령 실패);
             PQCLEAR (RES);
             exit_nicely (conn);

           /* pqclear pgresult가 더 이상 피할 필요가 없을 때마다
              메모리 누출 */
           PQCLEAR (RES);

           /* 데이터베이스의 시스템 카탈로그*/
           res = pqexec (conn, "select * from test1에 대한 mycursor binary cursor를 선언한다");
           if (pqresultstatus (res)! = pgres_command_ok) 
             fprintf (stderr, "선언 커서 명령 실패);
             PQCLEAR (RES);
             exit_nicely (conn);

           PQCLEAR (RES);

           RES = PQEXEC (Conn, "MyCursor에서 모두 가져 오기");
           if (pqresultstatus (res)! = pgres_tuples_ok) 
             fprintf (stderr, "모든 명령은 튜플을 올바르게 반환하지 않았다.);
             PQCLEAR (RES);
             exit_nicely (conn);

           i_fnum = pqfnumber (res, "i");
           d_fnum = pqfnumber (res, "d");
           p_fnum = pqfnumber (res, "p");

           for (i = 0; i <3; i ++) 
               printf ( "type [ %d] = %d, 크기 [ %d] = %d0,
                   i, pqftype (res, i),
                   i, pqfsize (res, i));

           for (i = 0; i <pqntuples (res); i ++) 
             int *ival;
             float *dval;
             int plen;
             다각형* Pval;
             /*/
             ival = (int*) pqgetValue (res, i, i_fnum);
             dval = (float*) pqgetValue (res, i, d_fnum);
             plen = pqgetlength (res, i, p_fnum);

             /* Plen은 길이 필드를 포함하지 않으므로 Varhdsz*/에 의해 증가해야합니다.
             pval = (다각형*) malloc (plen + varhdrsz);
             pval- size = plen;
             memmove ((char*) 및 pval- npts, pqgetValue (res, i, p_fnum), plen);
             printf ( "튜플 %d : got0, i);
             printf ( "i = ( %d 바이트) %d, 0,
                 pqgetlength (res, i, i_fnum), *ival);
             printf ( "d = ( %d 바이트) %f, 0,
                 pqgetlength (res, i, d_fnum), *dval);
             printf ( "p = (%d 바이트)%d 포인트 바운드 박스 = (hi =%f/%f, lo =%f,%f) 0,
                 pqgetlength (res, i, d_fnum),
                 pval- npts,
                 pval- boundbox.xh,
                 pval- boundbox.yh,
                 pval- boundbox.xl,
                 pval- boundbox.yl);

           PQCLEAR (RES);

           / * 포털 닫기 */
           RES = PQEXEC (Conn, "Close MyCursor");
           PQCLEAR (RES);

           / * 거래 종료 */
           res = pqexec (conn, "end");
           PQCLEAR (RES);

           / * 데이터베이스 연결을 닫고 정리 */
           pqfinish (conn);

이전 다음
버그 up 참조