0

I'm using a C program to talk to a Postgres database.

I'd like to create a method that will allow the user to type a custom query in the C program and see the results printed as Postgres would print in its command line client psql.

For other queries, I was able to use functions I found in the documentation. The trouble is that these only work because I know the amount of columns I need and the appropriate headers etc.

For example:

void* executeCustomQuery(const char* query){
    PGresult* res = PQexec(conn, query);
    //print all entries
    printf(PRODUCTS_TABLE_HEADER);
    int i;
    for (i = 0; i < PQntuples(res); i++){
        printf("| %s | %s | %s |", PQgetvalue(res, i, 0), PQgetvalue(res, i, 1), PQgetvalue(res, i, 2)); 
    }
    PQclear(res);
}

I can't use this code if I don't know what I'm getting back.

Does anyone know of any way to print out the direct results from Postgres?

5
  • One of the nice "features" of Postgres is it's free/open source software - take a look at the psql code. Commented Dec 12, 2013 at 15:21
  • RTFM. Everything's there: postgresql.org/docs/9.2/static/… Commented Dec 12, 2013 at 15:37
  • 2
    I found it shortly after posting. "RTM" would have done it though, no need for that "F" Commented Dec 12, 2013 at 15:39
  • It'd be nice if libpq exposed an easy "just print the query results" function. If you submitted one that formatted the query output to a filehandle (so you can pass stdout/stderr but don't have to) I'd be surprised if it wasn't accepted for future versions. Also, sometimes posting a patch is a great way to get someone to say "why don't you just use SecretUndocumentedFunctionNumber42"? Or say "ugh, that's a horrible way to do it, try <this patch>". Either way, win. Commented Dec 13, 2013 at 0:36
  • ... though it looks like such a function (PQprint) already exists. What about that doesn't meet your needs? Commented Dec 13, 2013 at 0:37

1 Answer 1

1

I ended up finding the method PQfname() which gives me the column names of the table. Using this I'm able to reconstruct the table with some for loops. It's not exactly what I was looking for but it's worth posting.

PGresult* res = PQexec(conn, query);
    char headerPrint[500];
        strcpy(headerPrint, "|");
        int h;
        for (h = 0; h < PQnfields(res); h++){
                strcat(headerPrint, " ");
                strcat(headerPrint, PQfname(res, h));
                strcat(headerPrint, " |");
                if (h == PQnfields(res)-1){
                    strcat(headerPrint, "\n");
                }
        }
        printf("%s", headerPrint);
        //Print content
        int i;
        char resultPrint[500];
        strcpy(resultPrint, "|");
        for (i = 0; i < PQntuples(res); i++){
            int j;
            for (j = 0; j < PQnfields(res); j++){
                strcat(resultPrint, " ");
                strcat(resultPrint, PQgetvalue(res, i, j));
                strcat(resultPrint, " |");
                //printf("%s %d %s", "Value of i is: ", i, "\n");
                //printf("%s %d %s", "Value of j is: ", j, "\n");
                //New line at the end
                if (j == PQnfields(res)-1){
                    strcat(resultPrint, "\n");
                }
            }
            printf("%s", resultPrint);
            strcpy(resultPrint, "|"); //Clear the current row and start over
        }
        PQclear(res);
Sign up to request clarification or add additional context in comments.

1 Comment

Readers should also look at PQprint .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.