Guest User

Untitled

a guest
Feb 9th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <libpq-fe.h>
  3.  
  4. void doSQL(PGconn *conn, char *command)
  5. {
  6. PGresult *result;
  7.  
  8. printf("%s\n", command);
  9.  
  10. result = PQexec(conn, command);
  11. printf("status is : %s\n", PQresStatus(PQresultStatus(result)));
  12. printf("#rows affected: %s\n", PQcmdTuples(result));
  13. printf("result message: %s\n", PQresultErrorMessage(result));
  14.  
  15. switch(PQresultStatus(result)) {
  16. case PGRES_TUPLES_OK:
  17. {
  18. int n = 0, m = 0;
  19. int nrows = PQntuples(result);
  20. int nfields = PQnfields(result);
  21. printf("number of rows returned = %d\n", nrows);
  22. printf("number of fields returned = %d\n", nfields);
  23. for(m = 0; m < nrows; m++) {
  24. for(n = 0; n < nfields; n++)
  25. printf(" %s = %s", PQfname(result, n),PQgetvalue(result,m,n));
  26. printf("\n");
  27. }
  28. }
  29. }
  30. PQclear(result);
  31. }
  32.  
  33. int main()
  34. {
  35. PGresult *result;
  36. PGconn *conn;
  37.  
  38. conn = PQconnectdb("host=localhost port=5432 dbname=pkulas user=pkulas password=");
  39. if(PQstatus(conn) == CONNECTION_OK) {
  40. printf("connection made\n");
  41. doSQL(conn, "CREATE TABLE nazwa (lp integer,imie varchar(40),nazwisko varchar(40),ulica varchar(40),numer integer,kod varchar(6),miejscowosc varchar(40),telefon varchar(20),email varchar(40),data_ur date)");
  42.  
  43. }
  44. else
  45. printf("connection failed: %s\n", PQerrorMessage(conn));
  46.  
  47. PQfinish(conn);
  48. return EXIT_SUCCESS;
  49. }
Add Comment
Please, Sign In to add comment