2011|08|
2013|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|05|06|07|08|09|10|11|12|
2016|01|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|

2017-12-15 Windows版のPostgreSQLを、D:の直下にインストールした場合、以下のコンパイルで通った [長年日記]

/*
  ソースコード→https://www29.atwiki.jp/m_shige1979/pages/605.html
 
  Windows版のPostgreSQLを、D:の直下にインストールした場合、以下のコンパイルで通った
 
  gcc -I"D:\PostgreSQL\pg96\include" pgsql_sample01.c -o pgsql_sample01.exe -L"D:\PostgreSQL\pg96\lib" -llibpq
 
 
  <期待される出力結果> 
 
  postgres=# \c db_hanabi
  db_hanabi=# \l
  データベース一覧
  名前    |  所有者  | エンコーディング |      照合順序      | Ctype(変換演算子)  |      アクセス権
  -----------+----------+------------------+--------------------+--------------------+-----------------------
  db_hanabi | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
  postgres  | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
  template0 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
  |          |                  |                    |                    | postgres=CTc/postgres
  template1 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
  |          |                  |                    |                    | postgres=CTc/postgres
  testdb1   | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
  
  
  db_hanabi=# SELECT * FROM testtable1;
  商品コード |        商品名         | 単価
  ------------+-----------------------+------
  1002       | 山の幸御膳            | 1200
  1003       | 海の幸御膳            | 1400
  1001       | 地方御膳              | 1000
  1004       | 七福神御膳            | 3000
  1005       | 松竹梅御膳            | 2000
  1006       | 鶴亀御膳              | 2500
 
*/
 
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
 
static void exit_nicely(PGconn *conn);
 
int main(void){
  
 
  const char *conninfo = "host=localhost user=postgres password=c-anemone dbname=db_hanabi";
  
  PGconn     *conn;
  PGresult   *res;
  int         nFields;
  int         i,j;
  
  printf("江端智一\n");
  
  /* データベースとの接続を確立する */
  conn = PQconnectdb(conninfo);
  
  /* バックエンドとの接続確立に成功したかを確認する */
  if (PQstatus(conn) != CONNECTION_OK){
	fprintf(stderr, "Connection to database failed: %s",
			PQerrorMessage(conn));
	exit_nicely(conn);
  }
  
  /* トランザクションブロックを開始する。 */
  res = PQexec(conn, "BEGIN");
  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
	fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
	PQclear(res);
	exit_nicely(conn);
  }
  
  /*
   * 不要になったら、メモリリークを防ぐためにPGresultをPQclearすべき。
   */
  PQclear(res);
  
  /*
   * データベースのシステムカタログpg_databaseから行を取り出す。
   */
  res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from testtable1");
  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
	fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
	PQclear(res);
	exit_nicely(conn);
  }
  PQclear(res);
 
  res = PQexec(conn, "FETCH ALL in myportal");
  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
	fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
	PQclear(res);
	exit_nicely(conn);
  }
  
  /* まず属性名を表示する。 */
  nFields = PQnfields(res);
  for (i = 0; i < nFields; i++){
	printf("%-15s", PQfname(res, i));
  }
  printf("\n\n");
  
  /* そして行を表示する。 */
  for (i = 0; i < PQntuples(res); i++) {
	for (j = 0; j < nFields; j++) {
	  printf("%-15s", PQgetvalue(res, i, j));
	}
	printf("\n");
  }
  
  PQclear(res);
  
  /* ポータルを閉ざす。ここではエラーチェックは省略した… */
  res = PQexec(conn, "CLOSE myportal");
  PQclear(res);
  
  /* トランザクションを終了する */
  res = PQexec(conn, "END");
  PQclear(res);
  
  /* データベースとの接続を閉じ、後始末を行う。 */
  PQfinish(conn);
  
  return 0;
}
 
static void exit_nicely(PGconn *conn){
  PQfinish(conn);
  exit(1);
}
syntax2html