Client 1—Connecting to the Server

Now that you understand the basic architecture of the ODBC API and you have defined a PostgreSQL data source, let's look at some sample code. This first client application connects to a database and then exits. Listing 12.1 provides an example that is much more complex than the sample clients in earlier chapters—ODBC is a complex API.

Listing 12.1. odbc/client1.c
 1 /* client1.c */ 2 3 #include <sql.h> 4 #include <sqlext.h> 5 6 #include <stdio.h> 7 8 typedef enum { FALSE, TRUE } bool; 9 10 int main( int argc, char * argv[] ) 11 { 12 SQLRETURN result; 13 SQLHENV envHandle; 14 SQLHDBC conHandle; 15 16 SQLAllocHandle( SQL_HANDLE_ENV, 17 SQL_NULL_HANDLE, 18 &envHandle ); 19 20 SQLSetEnvAttr( envHandle, 21 SQL_ATTR_ODBC_VERSION, ...

Get PostgreSQL, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.