ECPG

Uses your system voice; voice and rate in settings.

Snapshot

≈ 2 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

ECPG is the standard, in the PostgreSQL database built-in, client programming interface for embedding SQL in programs written in the C programming language. It provides the option for accessing the PostgreSQL database directly from the C code in the application, using SQL commands.

ECPG is the standard, in the PostgreSQL database built-in, client programming interface for embedding SQL in programs written in the C programming language. It provides the option for accessing the PostgreSQL database directly from the C code in the application, using SQL commands. // ... C code ... EXEC SQL ; // ... C code ... An example how to connect to a database: EXEC SQL CONNECT TO databasename[@hostname][:port] [AS connectionname] [USER username]; The embedded SQL part will be processed through ECPG preprocessor where SQL code will be replaced with the calls to the ecpg library (libecpg.a or libecpg.so). The file will be also preprocessed with ecpg, which converts it to a .c file according to the ANSI standards. Therefore, in the second step, the generated .c file can be directly compiled with a standard C compiler. Following command will create from the my_c_file_with_embedded_sql_commands.pcg file a my_c_file_with_embedded_sql_commands.c file, which can be processed further as a pure C code. $ ecpg my_c_file_with_embedded_sql_commands.pcg There is also source code of the ecpg available in the PostgreSQL Source Code git repository. Note: While compiling the preprocessed .c code, do not forget to link the ecpg library (libepcg), so that the generated calls can find their linked methods. EXEC SQL INSERT INTO tablename VALUES (:variablename); This can be used in any statement, INSERT statement was chosen just as a simple example for illustration. The above example shows how to pass a C variable to the SQL, but data can be passed also in the opposite direction: back to the application. The following example shows how to pass value from SQL back to the application's C variable. EXEC SQL BEGIN DECLARE SECTION; VARCHAR variablename; EXEC SQL END DECLARE SECTION; EXEC SQL SELECT columnname INTO :variablename FROM tablename; For simplicity, let's assume there is only one row in the table table name. This statement will insert the value of the column columnname into the variable variable. Every command that supports the INTO clause can be used in this way, for example the FETCH command.

Text derived from https://en.wikipedia.org/w/index.php?oldid=1302690385 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

5 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.