0

I have a table with two columns:

1) id SERIAL PRIMARY KEY 2) BYTEA

I am trying to fetch all the rows using PGresult * res = PQexecParams(conn, "select * from table",0,NULL,NULL,NULL,NULL,1); ==> The last argument = 1 specify results to be in binary format.

Due to the last argument, I am able to fetch BYTEA column properly but the "id" column is also returned in a format that I can't understand(probably in BYTEA format). Is there a way to convert the "id" value returned by the above mentioned PQexecParams to integer? I am using PQgetvalue API to fetch results.

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
Saurabh
  • 295
  • 3
  • 12
  • I'm not sure what you try to do, so just a hild guess - `PGresult * res = PQexecParams(conn, "select id::bit(32),second_column from table",0,NULL,NULL,NULL,NULL,1);` ?.. – Vao Tsun Apr 11 '17 at 12:25
  • [`PQgetvalue`](https://www.postgresql.org/docs/current/static/libpq-exec.html): *For data in binary format, the value is in the binary representation determined by the data type's typsend and typreceive functions. (The value is actually followed by a zero byte in this case too, but that is not ordinarily useful, since the value is likely to contain embedded nulls.)* (it most likely returns `int4*`) – pozs Apr 11 '17 at 12:47
  • "Select id:bit(32)" returns binary number "00000000000000000000000000000001" for integer 1 when I use this query from interactive terminal(psql) but PQgetvalue(res, 0, 0) returns 6542536(as char*) for integer 1. I still couldn't figure out how to convert 6542536 to integer 1. – Saurabh Apr 11 '17 at 13:54

0 Answers0