Реклама: |
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.
ECPGdebug(int on, FILE *stream) turns on debug logging if called with the first argument non-zero. Debug logging is done on stream. Most SQL statement logs its arguments and result.
The most important one (ECPGdo) that is called on almost all SQL statements 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.
ECPGstatus() 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 { char sqlcaid[8]; long sqlabc; long sqlcode; struct { int sqlerrml; char sqlerrmc[70]; } sqlerrm; char sqlerrp[8]; long sqlerrd[6]; /* 0: empty */ /* 1: empty */ /* 2: number of rows processed in an INSERT, UPDATE */ /* or DELETE statement */ /* 3: empty */ /* 4: empty */ /* 5: empty */ char sqlwarn[8]; /* 0: set to 'W' if at least one other is 'W' */ /* 1: if 'W' at least one character string */ /* value was truncated when it was */ /* stored into a host variable. */ /* 2: empty */ /* 3: empty */ /* 4: empty */ /* 5: empty */ /* 6: empty */ /* 7: empty */ char sqlext[8]; } 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 the line number in the source file.
List of errors that can occur:
Does not normally occur. This is a sign that your virtual memory is exhausted.
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.
This means that Postgres has returned more arguments 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 arguments than we have host variables. Perhaps you have too many host variables in the INTO :var1,:var2-list.
This means that the query has returned several lines but the variables specified are no arrays. 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 a 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 the host variable is of a bool type and the field in the Postgres database is neither 't' nor 'f'.
Postgres returned PGRES_EMPTY_QUERY, probably because the query indeed was empty.
The program tries to access a connection that does not exist.
The program tries to access a connection that does exist but is not open.
The statement you are trying to use has not been prepared.
Some Postgres error. The message contains the error message from the Postgres backend.
Postgres signalled to us that we cannot start, commit or rollback the transaction.
The connect to the database did not work.
This is a "normal" error that tells you that what you are quering cannot be found or we have gone through the cursor.