PostgreSQL | ||
---|---|---|
Prev | Chapter 43. ecpg - Embedded SQL in C | Next |
This section describes how to use the egpc tool.
The preprocessor is called ecpg. After installation it resides in the Postgres bin/ directory.
The ecpg library is called libecpg.a or libecpg.so. Additionally, the library uses the libpq library for communication to the Postgres server so you will have to link your program with -lecpg -lpq.
The library has some methods that are "hidden" but that could prove very useful sometime.
If this is called, with the first argument non-zero, then debuglogging is turned on. Debuglogging is done on stream. Most SQL statement logs its arguments and result.
The most important one (ECPGdo) that is called on all SQL statements except EXEC SQL COMMIT, EXEC SQL ROLLBACK, EXEC SQL CONNECT logs both its expanded string, i.e. the string with all the input variables inserted, and the result from the Postgres server. This can be very useful when searching for errors in your SQL statements.
This method returns TRUE if we are connected to a database and FALSE if not.
To be able to detect errors from the Postgres server you include a line like
exec sql include sqlca;in the include section of your file. This will define a struct and a variable with the name sqlca as following:
struct sqlca { int sqlcode; struct { int sqlerrml; char sqlerrmc[1000]; } sqlerrm; } sqlca;
If an error occured in the last SQL statement then sqlca.sqlcode will be non-zero. If sqlca.sqlcode is less that 0 then this is some kind of serious error, like the database definition does not match the query given. If it is bigger than 0 then this is a normal error like the table did not contain the requested row.
sqlca.sqlerrm.sqlerrmc will contain a string that describes the error. The string ends with “line 23.” where the line is the line number in the source file (actually the file generated by the preprocessor but I hope I can fix this to be the line number in the input file.)
List of errors that can occur:
Does not normally occur. This is a sign that the preprocessor has generated something that the library does not know about. Perhaps you are running incompatible versions of the preprocessor and the library.
The preprocessor has goofed up and generated some incorrect code.
The preprocessor has goofed up and generated some incorrect code.
Postgres signalled to us that we cannot open the connection.
Some Postgres error. The message contains the error message from the Postgres backend.
This is a "normal" error that tells you that what you are quering cannot be found or we have gone through the cursor.
This means that the query has returned several lines. The SELECT you made probably was not unique.
This means that the host variable is of an int type and the field in the Postgres database is of another type and contains a value that cannot be interpreted as an int. The library uses strtol for this conversion.
This means that the host variable is of an unsigned int type and the field in the Postgres database is of another type and contains a value that cannot be interpreted as an unsigned int. The library uses strtoul for this conversion.
This means that the host variable is of an float type and the field in the Postgres database is of another type and contains a value that cannot be interpreted as an float. The library uses strtod for this conversion.
This means that Postgres has returned more records than we have matching variables. Perhaps you have forgotten a couple of the host variables in the INTO :var1,:var2-list.
This means that Postgres has returned fewer records than we have host variables. Perhaps you have to many host variables in the INTO :var1,:var2-list.
Postgres returned PGRES_EMPTY_QUERY.
This means that Postgres returned on of the errors PGRES_NONFATAL_ERROR, PGRES_FATAL_ERROR or PGRES_BAD_RESPONSE. Which one and why is explained in the message.
Postgres returns something that the library does not know how to handle. This is probably because the version of Postgres does not match the version of the ecpg library.
Error during COMMIT. EXEC SQL COMMIT is translated to an end operation in Postgres and that is the operation that could not be performed.
Error during ROLLBACK. EXEC SQL ROLLBACK is translated to an abort operation in Postgres and that is the operation that could not be performed.
The connect to the database did not work.
Prev | Home | Next |
The Concept | Up | Limitations |