list of databases in C ? libpq ?
Hello all,
I need to write an application in C to read the list of databases
currently in the server. very much like a "psql -l"...
but I need it in C ! I never used C before to access PG.
the libpq API seems a bit scary ! Is there anything, shipped with
postgresql, other than libpq that would make my life simpler ?
thanks a lot
Joao
On Fri, May 7, 2010 at 11:18, Joao Ferreira gmail
<joao.miguel.c.ferreira@gmail.com> wrote:
Hello all,
I need to write an application in C to read the list of databases
currently in the server. very much like a "psql -l"...
select datname as database from pg_database;
but I need it in C ! I never used C before to access PG.
the libpq API seems a bit scary ! Is there anything, shipped with
postgresql, other than libpq that would make my life simpler ?
Maybe ecpg http://www.postgresql.org/docs/current/static/ecpg.html ? I
also hear good things about libpqtypes making some things easier
http://pgfoundry.org/projects/libpqtypes/.
On May 7, 2010, at 10:18 AM, Joao Ferreira gmail wrote:
Hello all,
I need to write an application in C to read the list of databases
currently in the server. very much like a "psql -l"...but I need it in C ! I never used C before to access PG.
the libpq API seems a bit scary !
It's easier than it looks, really:
PGconn *db;
PGresult *result;
int i;
db = PQconnectdb("host=127.0.0.1 dbname=template1 user=joao password=foo");
if(0 == db || PQstatus(db) != CONNECTION_OK) {
fprintf(stderr, "Failed to connect to db: %s", db ? PQerrorMessage(db) : "unknown error\n");
exit(1);
}
result = PQexec(db, "select datname from pg_catalog.pg_database");
if(0 == result || PQresultStatus(result) != PGRES_TUPLES_OK) {
fprintf(stderr, "Failed to run query: %s", result ? PQresultErrorMessage(result) : "unknown error\n");
exit(1);
}
for(i=0; i < PQntuples(result); ++i) {
printf("%s\n", PQgetvalue(result, i, 0));
}
PQclear(result);
PQfinish(db);
(Untested)
Is there anything, shipped with
postgresql, other than libpq that would make my life simpler ?
Not for C, I don't think. Other languages (C++, perl ...) have bindings that simplify some things.
Cheers,
Steve
On 5/7/2010 1:48 PM, Alex Hunsaker wrote:
On Fri, May 7, 2010 at 11:18, Joao Ferreira gmail
<joao.miguel.c.ferreira@gmail.com> wrote:Hello all,
I need to write an application in C to read the list of databases
currently in the server. very much like a "psql -l"...
The first example in the online docs does exactly that...
http://www.postgresql.org/docs/8.1/static/libpq-example.html
$ cc -I /usr/include/postgresql-8.4/ -lpq libpq.c -o libpqex
$ ./libpqex dbname=test
datname datdba encoding datcollate datctype datistemplate datallowconn
datconnlimit datlastsysoid datfrozenxid dattablespace datconfig datacl
template1 10 6 en_US.UTF-8 en_US.UTF-8 t t -1
11563 648 1663 {=c/postgres,postgres=CTc/postgres}
template0 10 6 en_US.UTF-8 en_US.UTF-8 t f -1
11563 648 1663 {=c/postgres,postgres=CTc/postgres}
postgres 10 6 en_US.UTF-8 en_US.UTF-8 f t -1
11563 648 1663
test 16384 6 en_US.UTF-8 en_US.UTF-8 f t -1
11563 648 1663
sigcap 16438 0 en_US.UTF-8 en_US.UTF-8 f t -1
11563 648 1663