Embedded SQL C - Lecture 01232012
Embedded SQL C - Lecture 01232012
1. PostgreSQL provides a library for C - libpq. It has functions - PQconnectdb, PQexec, PQfinish etc.
http://www.postgresql.org/docs/7.4/static/libpq.html
2. Connect to VPN. It is needed because our database server does not accept any connections from
outside the network. Check the links below for how to connect to VPN.
http://cat.pdx.edu/windows/connecting-to-the-vpn-on-windows.html
http://cat.pdx.edu/linux/connecting-to-the-vpn-on-linux-2.html
http://cat.pdx.edu/mac/connecting-to-the-vpn-from-mac-2.html
if (PQstatus(conn) != CONNECTION_OK) {
printf("Connection to database failed: %s",
PQerrorMessage(conn));
} else {
printf("Connection Successful!\n");
}
/* Execute a query */
res = PQexec(conn, "SELECT * FROM movies");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
printf("SELECT failed: %s", PQerrorMessage(conn));
//PQclear(res);
} else {
printf("SELECT Successful!\n");
}
tuples = PQntuples(res);
printf ("Number of tuples: %d\n\n", tuples);
/* first, print out the attribute names */
nFields = PQnfields(res);
for (i = 0; i < 2; i++)
printf("%-50s", PQfname(res, i));
printf("\n");
for (i = 0; i < 60; i++)
printf("%c", '-');
printf("\n");
/* next, print out the rows */
for (i = 0; i < PQntuples(res); i++)
{
for (j = 0; j < 2; j++)
printf("%-15s", PQgetvalue(res, i, j));
printf("\n");
}
PQclear(res);
/* close the connection to the database and cleanup */
PQfinish(conn);
getchar();
}