PQresultStatus

PQresultStatusで、PQexecの結果を取得する。

https://www.postgresql.jp/document/7.3/programmer/libpq-exec.html

    /* SQL実行 */
    res = PQexec(con, ""); /* PQexecでSQL文"SELECT * FROM user_profile;"を実行. */
    type = PQresultStatus(res); /* PQresultStatusで結果を取得. */
    if (type == PGRES_TUPLES_OK){ /* 成功 */
      printf("PGRES_TUPLES_OK\n"); /* "PGRES_TUPLES_OK"と出力. */
      PQclear(res); /* resの解放 */
      PQfinish(con); /* PQfinishでconの接続を終了. */
      return 0; /* 0を返して正常終了. */
    }
    else if (type == PGRES_EMPTY_QUERY){ /* クエリが空. */
      printf("PGRES_EMPTY_QUERY\n"); /* "PGRES_EMPTY_QUERY"と出力. */
      PQclear(res); /* resの解放 */
      PQfinish(con); /* PQfinishでconの接続を終了. */
      return -1; /* -1を返して異常終了. */
    }
    else if (type == PGRES_FATAL_ERROR){ /* SQL文がおかしい場合など. */
      printf("PGRES_FATAL_ERROR\n"); /* "PGRES_FATAL_ERROR"と出力. */
      PQclear(res); /* resの解放 */
      PQfinish(con); /* PQfinishでconの接続を終了. */
      return -1; /* -1を返して異常終了. */
    }
    else{ /* その他の失敗 */
      printf("NG\n"); /* "NG"と出力. */
      PQclear(res); /* resの解放 */
      PQfinish(con); /* PQfinishでconの接続を終了. */
      return -1; /* -1を返して異常終了. */
    }

SQL文が""という空の場合、

[bg1@localhost PQresultStatus]$ vi main.c
[bg1@localhost PQresultStatus]$ gcc main.c -o main -lpq
[bg1@localhost PQresultStatus]$ ./main
CONNECTION_OK
PGRES_EMPTY_QUERY
[bg1@localhost PQresultStatus]$

PGRES_EMPTY_QUERYが返る。

    /* SQL実行 */
    res = PQexec(con, "SELECT * FROM"); /* PQexecでSQL文"SELECT * FROM user_profile;"を実行. */
    type = PQresultStatus(res); /* PQresultStatusで結果を取得. */
    if (type == PGRES_TUPLES_OK){ /* 成功 */
      printf("PGRES_TUPLES_OK\n"); /* "PGRES_TUPLES_OK"と出力. */
      PQclear(res); /* resの解放 */
      PQfinish(con); /* PQfinishでconの接続を終了. */
      return 0; /* 0を返して正常終了. */
    }
    else if (type == PGRES_EMPTY_QUERY){ /* クエリが空. */
      printf("PGRES_EMPTY_QUERY\n"); /* "PGRES_EMPTY_QUERY"と出力. */
      PQclear(res); /* resの解放 */
      PQfinish(con); /* PQfinishでconの接続を終了. */
      return -1; /* -1を返して異常終了. */
    }
    else if (type == PGRES_FATAL_ERROR){ /* SQL文がおかしい場合など. */
      printf("PGRES_FATAL_ERROR\n"); /* "PGRES_FATAL_ERROR"と出力. */
      PQclear(res); /* resの解放 */
      PQfinish(con); /* PQfinishでconの接続を終了. */
      return -1; /* -1を返して異常終了. */
    }
    else{ /* その他の失敗 */
      printf("NG\n"); /* "NG"と出力. */
      PQclear(res); /* resの解放 */
      PQfinish(con); /* PQfinishでconの接続を終了. */
      return -1; /* -1を返して異常終了. */
    }

SQL文がおかしい場合には、

[bg1@localhost PQresultStatus]$ vi main.c
[bg1@localhost PQresultStatus]$ gcc main.c -o main -lpq
[bg1@localhost PQresultStatus]$ ./main
CONNECTION_OK
PGRES_FATAL_ERROR
[bg1@localhost PQresultStatus]$

PGRES_FATAL_ERRORが返る。

正しいSQL文なら、

[bg1@localhost PQresultStatus]$ vi main.c
[bg1@localhost PQresultStatus]$ gcc main.c -o main -lpq
[bg1@localhost PQresultStatus]$ ./main
CONNECTION_OK
PGRES_TUPLES_OK
[bg1@localhost PQresultStatus]$

PGRES_TUPLES_OKが返る。

Sample/libpq/PQresultStatus/PQresultStatus/src/PQresultStatus at master · bg1bgst333/Sample · GitHub