Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. //wyswietlanie zawartosci tabeli wraz z rozmiarem pola
  2. #include <stdlib.h>
  3. #include <libpq-fe.h>
  4.  
  5. void doSQL(PGconn *conn, char *command)
  6. {
  7. PGresult *result;
  8.  
  9. // printf("%s\n", command);
  10.  
  11. result = PQexec(conn, command);
  12. // printf("status is %s\n", PQresStatus(PQresultStatus(result)));
  13. // printf("#rows affected %s\n", PQcmdTuples(result));
  14. // printf("result message: %s\n", PQresultErrorMessage(result));
  15.  
  16. switch(PQresultStatus(result)) {
  17. case PGRES_TUPLES_OK:
  18. {
  19. int m, n;
  20. int nrows = PQntuples(result);
  21. int nfields = PQnfields(result);
  22. printf("number of rows returned = %d\n", nrows);
  23. printf("number of fields returned = %d\n", nfields);
  24. for(m = 0; m < nrows; m++) {
  25. for(n = 0; n < nfields; n++)
  26. printf(" %s = %s(%d),",
  27. PQfname(result, n),
  28. PQgetvalue(result, m, n),
  29. // rozmiar pola w bajtach
  30. PQgetlength(result, m, n));
  31. printf("\n");
  32. }
  33. }
  34. }
  35. PQclear(result);
  36. }
  37.  
  38. int main()
  39. {
  40. PGresult *result;
  41. PGconn *conn;
  42.  
  43. conn = PQconnectdb("host=localhost port=5432 dbname=nazwa user=login password=haslo");
  44. if(PQstatus(conn) == CONNECTION_OK) {
  45. printf("connection made\n");
  46.  
  47. doSQL(conn, "DROP TABLE number");
  48. doSQL(conn, "CREATE TABLE number(value INTEGER,name VARCHAR)");
  49. doSQL(conn, "INSERT INTO number values(42, 'The Answer')");
  50. doSQL(conn, "INSERT INTO number values(29, 'My Age')");
  51. doSQL(conn, "INSERT INTO number values(29, 'Anniversary')");
  52. doSQL(conn, "INSERT INTO number values(66, 'Clickety-Click')");
  53. doSQL(conn, "SELECT * FROM number");
  54. }
  55. else
  56. printf("connection failed: %s\n", PQerrorMessage(conn));
  57.  
  58. PQfinish(conn);
  59. return EXIT_SUCCESS;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement