ecpg [-v] [-t] [-I include-path] [-o outfile] file...
The files to be processed.
Specify an additional include path. Defaults are ., /usr/include/pgsql (the PostgreSQL include path, which is defined at compile time), and /usr/include.
Specifies that ecpg should write all its output to outfile. If no such option is given the output is written to name.c, assuming the input file was named name.pgc. If the input file does have the expected .pgc suffix, then the output file will have .pgc appended to the input file name.
Turn on auto-commit of transactions. In this mode, each query is automatically committed unless it is inside an explicit transaction block. In the default mode, queries are committed only when exec sql commit is issued.
Print version information.
ecpg is an embedded SQL preprocessor for the C language and the PostgreSQL. It enables development of C programs with embedded SQL code.
An embedded SQL source file must be pre-processed before compilation:
ecpg [ -d ] [ -o file ] file.pgc |
You may want to redirect the preprocessor output to a log file.
The preprocessor will prepend two directives to the source:
#include <ecpgtype.h> #include <ecpglib.h> |
Variables declared within ecpg source code must be prepended with:
EXEC SQL BEGIN DECLARE SECTION; |
Similarly, variable declaration sections must terminate with:
EXEC SQL END DECLARE SECTION; |
Multiple variables may be declared on a single line:
|
The SQL communication area is defined with:
EXEC SQL INCLUDE sqlca; |
The sqlca is in lowercase. While SQL convention may be followed, that is, using uppercase to separate embedded SQL from C statements, sqlca (which includes the sqlca.h header file) must be lowercase. This is because the EXEC SQL prefix indicates that this inclusion will be parsed by ecpg. ecpg observes case sensitivity (SQLCA.h will not be found). EXEC SQL INCLUDE can be used to include other header files as long as case sensitivity is observed. |
The sqlprint command is used with the EXEC SQL WHENEVER statement to turn on error handling throughout the program:
EXEC SQL WHENEVER sqlerror sqlprint; |
EXEC SQL WHENEVER not found sqlprint; |
This is not an exhaustive example of usage for the EXEC SQL WHENEVER statement. Further examples of usage may be found in SQL manuals. |
One connects to a database using the following:
EXEC SQL CONNECT TO dbname; |
Specifying a server and port name in the connect statement is also possible. The syntax is:
dbname[@server][:port] |
<tcp|unix>:postgresql://server[:port][/dbname][?options] |
In general, SQL queries acceptable to other applications such as psql can be embedded into your C code. Here are some examples of how to do that.
Create Table:
EXEC SQL CREATE TABLE foo (number int4, ascii char(16)); EXEC SQL CREATE UNIQUE index num1 on foo(number); EXEC SQL COMMIT; |
Insert:
EXEC SQL INSERT INTO foo (number, ascii) VALUES (9999, 'doodad'); EXEC SQL COMMIT; |
Delete:
EXEC SQL DELETE FROM foo WHERE number = 9999; EXEC SQL COMMIT; |
Singleton Select:
EXEC SQL SELECT foo INTO :FooBar FROM table1 WHERE ascii = 'doodad'; |
Select using Cursors:
EXEC SQL DECLARE foo_bar CURSOR FOR SELECT number, ascii FROM foo ORDER BY ascii; EXEC SQL FETCH foo_bar INTO :FooBar, DooDad; ... EXEC SQL CLOSE foo_bar; EXEC SQL COMMIT; |
Updates:
EXEC SQL UPDATE foo SET ascii = 'foobar' WHERE number = 9999; EXEC SQL COMMIT; |