database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 2319 게시물 읽기
No. 2319
Re: 저기.. postgresql과 C언어가 연동되는 게시판 소스같은 것은 없나요?
작성자
정재익
작성일
2001-02-19 16:12
조회수
4,379

개인적인 생각으로는 PostgreSQL 메뉴얼의 pqlib 부분을 참조하시는 것이 가장 좋을 것 같습니다.

 

다음에 pq_lib 을 이용한 간단한 예제가 있습니다. 이것은 postgresql 로 접속하여 현재 생성되어 있는 database 들의 이름을 추출해서 화면에 보여주는 프로그램입니다.

==================================================

 

#include <stdio.h>

#include "libpq/fe.h"

#include "libpq++.h"

 

void

exit_nicely(PGconn *conn)

{

PQfinish(conn);

exit(1);

}

 

main()

{

char *pghost,

*pgport,

*pgoptions,

*pgtty;

char *dbName;

int nFields;

int i,

j;

 

/* FILE *debug; */

 

PGconn *conn;

PGresult *res;

 

/*

* begin, by setting the parameters for a backend connection if the

* parameters are null, then the system will try to use reasonable

* defaults by looking up environment variables or, failing that,

* using hardwired constants

*/

pghost = NULL; /* host name of the backend server */

pgport = NULL; /* port of the backend server */

pgoptions = NULL; /* special options to start up the backend

* server */

pgtty = NULL; /* debugging tty for the backend server */

dbName = "template1";

 

/* 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)

{

fprintf(stderr, "Connection to database '%s' failed.\n", dbName);

fprintf(stderr, "%s", PQerrorMessage(conn));

exit_nicely(conn);

}

 

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

/* PQtrace(conn, debug); */

 

/* start a transaction block */

res = PQexec(conn, "BEGIN");

if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)

{

fprintf(stderr, "BEGIN command failed\n");

PQclear(res);

exit_nicely(conn);

}

/*

* should PQclear PGresult whenever it is no longer needed to avoid

* memory leaks

*/

PQclear(res);

 

/*

* fetch instances from the pg_database, the system catalog of

* databases

*/

res = PQexec(conn, "DECLARE mycursor CURSOR FOR select * from pg_database");

if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)

{

fprintf(stderr, "DECLARE CURSOR command failed\n");

PQclear(res);

exit_nicely(conn);

}

PQclear(res);

res = PQexec(conn, "FETCH ALL in mycursor");

if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)

{

fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");

PQclear(res);

exit_nicely(conn);

}

 

/* first, print out the attribute names */

nFields = PQnfields(res);

for (i = 0; i < nFields; i++)

printf("%/15s", PQfname(res, i));

printf("\n\n");

 

/* next, print out the instances */

for (i = 0; i < PQntuples(res); i++)

{

for (j = 0; j < nFields; j++)

printf("%/15s", PQgetvalue(res, i, j));

printf("\n");

}

PQclear(res);

 

/* close the cursor */

res = PQexec(conn, "CLOSE mycursor");

PQclear(res);

 

/* commit the transaction */

res = PQexec(conn, "COMMIT");

PQclear(res);

 

/* close the connection to the database and cleanup */

PQfinish(conn);

 

/* fclose(debug); */

return 0;

 

}

 

>>이태웅 님께서 쓰시길<<

 

:: 허접한거라도.. 어떻게 접속을 하고..

:: 각종 데이터를 어떻게 사용하는지 보고 싶은데.. //;

:: 행복하세요~

[Top]
No.
제목
작성자
작성일
조회
2324IPV6 인가?
신현호
2001-02-20
3191
2327┕>Re: IPV6 인가?
정재익
2001-02-20 09:59:53
3771
2323Sequence 의 최대 값은 얼마일까요?
정재익
2001-02-20
3515
2320아~ postgresql는 공짜인데.. 왜.. mysql보다 인기가 없는 것일까?
이태웅
2001-02-19
4585
2321┕>Re: 아~ postgresql는 공짜인데.. 왜.. mysql보다 인기가 없는 것일까?
정재익
2001-02-19 17:08:35
3891
2322 ┕>Re: Re: 아~ postgresql는 공짜인데.. 왜.. mysql보다 인기가 없는 것일까?
이태웅
2001-02-19 17:18:56
3655
2317저기.. postgresql과 C언어가 연동되는 게시판 소스같은 것은 없나요?
이태웅
2001-02-19
4200
2319┕>Re: 저기.. postgresql과 C언어가 연동되는 게시판 소스같은 것은 없나요?
정재익
2001-02-19 16:12:03
4379
2316백업시 에러가 납니다.
조석연
2001-02-19
3313
2318┕>Re: 백업시 에러가 납니다.
정재익
2001-02-19 15:43:19
3636
2314역시 문서가 중요하군요...
배찬일
2001-02-19
3176
2315┕>Re: 역시 문서가 중요하군요...
정재익
2001-02-19 11:12:36
3537
2312PostgreSQL에 관련된 함수가 궁금해서요....(MySQL 사용자)
초보자
2001-02-18
3366
2313┕>Re: PostgreSQL에 관련된 함수가 궁금해서요....(MySQL 사용자)
정재익
2001-02-18 14:23:23
3898
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다