Varios demostraciones de uso de postgresql en distintos lenguajes de programación.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

postgresql_demo.c 866B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <stdio.h>
  2. #include <postgresql/libpq-fe.h>
  3. int main(int argc, char *argv[]) {
  4. PGconn *conn;
  5. PGresult *res;
  6. int i;
  7. int j;
  8. conn = PQsetdbLogin("localhost", "5432", "", "", "database", "user", "password");
  9. if (PQstatus(conn) != CONNECTION_OK) {
  10. fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
  11. PQfinish(conn);
  12. return 1;
  13. }
  14. res = PQexec(conn, "SELECT * FROM turista");
  15. if (PQresultStatus(res) != PGRES_TUPLES_OK) {
  16. fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
  17. PQclear(res);
  18. PQfinish(conn);
  19. return 1;
  20. }
  21. for (i = 0; i < PQnfields(res); i++) {
  22. printf("%-20s", PQfname(res, i));
  23. }
  24. printf("\n\n");
  25. for (i = 0; i < PQntuples(res); i++){
  26. for (j = 0; j < PQnfields(res); j++) {
  27. printf("%-20s", PQgetvalue(res, i, j));
  28. }
  29. printf("\n");
  30. }
  31. PQclear(res);
  32. PQfinish(conn);
  33. return 0;
  34. }