-
Notifications
You must be signed in to change notification settings - Fork 0
/
posgresql.c
44 lines (42 loc) · 1.31 KB
/
posgresql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <postgresql/libpq-fe.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
void funcionPostgresql(char IPPostgrsql[], char PuertoPostgresql[],char bd[],char user[], char pwd[],int idsockc, char query[], char *response[])
{
PGconn *conn;
PGresult *res;
int i,j;
char respuesta[1024];
memset(respuesta,0,1024);
conn = PQsetdbLogin(IPPostgrsql,PuertoPostgresql,NULL,NULL,bd,user,pwd);
if (PQstatus(conn) != CONNECTION_BAD){
puts("entre a la BD");
printf("Salida: %s\n",query);
res = PQexec(conn, query);
puts("segmentation");
if (res != NULL && PGRES_TUPLES_OK == PQresultStatus(res))
{
puts("Res no es null y pgrestuples OK.....");
for (i = 0 ; i <= PQntuples(res)-1; i++)
{
for (j = 0 ; j < PQnfields(res); j++)
{
strcat(respuesta,PQgetvalue(res,i,j));
strcat(respuesta,"//");
}
strcat(respuesta,".\n");
}
strcat(respuesta,"\0");
PQclear(res);
}
}
PQfinish(conn);
strcpy(response, respuesta);
}