initial commit

This commit is contained in:
2018-12-21 16:03:06 -03:00
commit 0bb81e5dee
7 changed files with 196 additions and 0 deletions

20
c/meson.build Normal file
View File

@@ -0,0 +1,20 @@
project('postgresqldemo',
'c',
version : '1.0.0',
license : 'BSD-3',
default_options : [ 'b_ofast=if-release', 'b_march_native=if-release', 'b_ndebug=if-release' ]
)
#gtk_dep = dependency('gtk+-3.0', version : '>=3.0.0')
# gmodule-export-2.0 is needed to connect the handlers
#gmodule_dep = dependency('gmodule-export-2.0', version : '>=2.0')
pg_dep = dependency('libpq', version : '>=8.0')
cc = meson.get_compiler('c')
sources = ['postgresql_demo.c']
exe = executable('postgresql_demo',
sources,
dependencies : [pg_dep],
install : false)

40
c/postgresql_demo.c Normal file
View File

@@ -0,0 +1,40 @@
#include <libpq-fe.h>
int main(int argc, char *argv[]) {
PGconn *conn;
PGresult *res;
int i;
int j;
conn = PQsetdbLogin("localhost", "5432", "", "", "database", "user", "password");
if (PQstatus(conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
PQfinish(conn);
return 1;
}
res = PQexec(conn, "SELECT * FROM turista");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
return 1;
}
for (i = 0; i < PQnfields(res); i++) {
printf("%-20s", PQfname(res, i));
}
printf("\n\n");
for (i = 0; i < PQntuples(res); i++){
for (j = 0; j < PQnfields(res); j++) {
printf("%-20s", PQgetvalue(res, i, j));
}
printf("\n");
}
PQclear(res);
PQfinish(conn);
return 0;
}