Database Reference
In-Depth Information
#include<sys/types.h>
#include<stdio.h>
EXEC SQL BEGIN DECLARE SECTION;
chartarget_tcp[] = "tcp:postgresql://127.0.01:5432/postgres";
chartarget_unix[] = "unix:postgresql://localhost:5432/postgres";
char *version;
EXEC SQL END DECLARE SECTION;
int main(int argc, char **argv)
{
/* Establish connection using TCP */
EXEC SQL CONNECT TO :target_tcp AS connection_tcp USER postgres;
/* Establish connection using Unix domain socket */
EXEC SQL CONNECT TO :target_unix AS connection_unix USER
postgres;
/* Run the command using target_tcp connection */
EXEC SQL AT connection_tcp SELECT version() INTO :version;
fprintf(stdout, "PostgreSQL Version Using UNIX domain socket:
%s\n", version);
/* Switch connection to target_unix */
EXEC SQL SET CONNECTION connection_unix;
fprintf(stdout, "PostgreSQL Version Using TCP: %s\n", version);
/* Disconnect connection_tcp connection */
EXEC SQL DISCONNECT connection_tcp;
/* Disconnect all will close the connection_unix which is still
open */
EXEC SQL DISCONNECT ALL;
return 0;
}
You do not need to include ecpglib.h in your pgc program. The
ECPG program will add the ECPG header iles in generated .c ile.
Running SQL commands
We have discussed how to connect to the PostgreSQL server using ECPG. After
connection, we can run the SQL commands. This command can be a DDL or DML
statement. The syntax to run SQL commands on the PostgreSQL server is as follows:
EXEC SQL sql_command
 
Search WWH ::




Custom Search