From: PG Doc comments form <noreply@postgresql.org>
To: pgsql-docs@lists.postgresql.org
Cc: duncan.reitboeck@gmail.com
Subject: Crash when using PQgetvalue
Date: Mon, 04 Jun 2018 15:26:21 +0000
Message-ID: <152812598158.26719.17918140296757658987@wrigleys.postgresql.org> (raw)
The following documentation comment has been logged on the website:
Page: https://www.postgresql.org/docs/9.5/static/libpq.html
Description:
Hello,
I am not sure if I am doing something wrong or there is a bug in the code
but I am getting a strange issue. When I try to run the following code I get
a crash when attempting to accessing the values using PQgetvalue.
I was able to print off all the column names using PQfname. If I replace
PQgetvalue with PQgetlength the correct rows, fields and lengths are
printed. Checking PQgetisnull displayed the value as not null. I also did a
few checks using PQstatus and PQresultStatus to make sure the connection or
result set didn't mess up somehow in the middle of the code.
PGconn *con;
PGresult *pg_result;
int i = 0;
int j = 0;
int cols = 0;
int rows = 0;
con = PQconnectdb("host=localhost user=postgres password=test
dbname=test");
if(PQstatus(con) != CONNECTION_OK)
{
printf("Could not connect to PostgreSQL database!");
fflush(stdout);
}
else
{
g_print("Connected.\n");
pg_result = PQexec(con, "SELECT * FROM test");
if(PQresultStatus(pg_result) == PGRES_TUPLES_OK)
{
rows = PQntuples(pg_result);
cols = PQnfields(pg_result);
for(i = 0; i < rows; ++i)
{
for(j = 0; j < cols; ++j)
{
printf("Row: %d - Col: %d = %s\n", i, j, PQgetvalue(pg_result, i,
j)); //This is where the crash occurs. Also happens if not attempting to
print and simply getting the value or calling the function.
fflush(stdout);
}
}
}
PQclear(pg_result);
PQfinish(con);
con = NULL;
}
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-docs@postgresql.org
Cc: noreply@postgresql.org, pgsql-docs@lists.postgresql.org, duncan.reitboeck@gmail.com
Subject: Re: Crash when using PQgetvalue
In-Reply-To: <152812598158.26719.17918140296757658987@wrigleys.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox