ecpg.sgml
来自「PostgreSQL7.4.6 for Linux」· SGML 代码 · 共 1,854 行 · 第 1/4 页
SGML
1,854 行
<varlistentry> <term><literal>SQLPRINT</literal></term> <listitem> <para> Print a message to standard error. This is useful for simple programs or during prototyping. The details of the message cannot be configured. </para> </listitem> </varlistentry> <varlistentry> <term><literal>STOP</literal></term> <listitem> <para> Call <literal>exit(1)</literal>, which will terminate the program. </para> </listitem> </varlistentry> <varlistentry> <term><literal>BREAK</literal></term> <listitem> <para> Execute the C statement <literal>break</literal>. This should only be used in loops or <literal>switch</literal> statements. </para> </listitem> </varlistentry> <varlistentry> <term><literal>CALL <replaceable>name</replaceable> (<replaceable>args</replaceable>)</literal></term> <term><literal>DO <replaceable>name</replaceable> (<replaceable>args</replaceable>)</literal></term> <listitem> <para> Call the specified C functions with the specified arguments. </para> </listitem> </varlistentry> </variablelist> The SQL standard only provides for the actions <literal>CONTINUE</literal> and <literal>GOTO</literal> (and <literal>GO TO</literal>). </para> <para> Here is an example that you might want to use in a simple program. It prints a simple message when a warning occurs and aborts the program when an error happens.<programlisting>EXEC SQL WHENEVER SQLWARNING SQLPRINT;EXEC SQL WHENEVER SQLERROR STOP;</programlisting> </para> <para> The statement <literal>EXEC SQL WHENEVER</literal> is a directive of the SQL preprocessor, not a C statement. The error or warning actions that it sets apply to all embedded SQL statements that appear below the point where the handler is set, unless a different action was set for the same condition between the first <literal>EXEC SQL WHENEVER</literal> and the SQL statement causing the condition, regardless of the flow of control in the C program. So neither of the two following C program excerpts will have the desired effect.<programlisting>/* * WRONG */int main(int argc, char *argv[]){ ... if (verbose) { EXEC SQL WHENEVER SQLWARNING SQLPRINT; } ... EXEC SQL SELECT ...; ...}</programlisting><programlisting>/* * WRONG */int main(int argc, char *argv[]){ ... set_error_handler(); ... EXEC SQL SELECT ...; ...}static void set_error_handler(void){ EXEC SQL WHENEVER SQLERROR STOP;}</programlisting> </para> </sect2> <sect2> <title>sqlca</title> <para> For a more powerful error handling, the embedded SQL interface provides a global variable with the name <varname>sqlca</varname> that has the following structure:<programlisting>struct{ char sqlcaid[8]; long sqlabc; long sqlcode; struct { int sqlerrml; char sqlerrmc[70]; } sqlerrm; char sqlerrp[8]; long sqlerrd[6]; char sqlwarn[8]; char sqlstate[5];} sqlca;</programlisting> (In a multithreaded program, every thread automatically gets its own copy of <varname>sqlca</varname>. This works similar to the handling of the standard C global variable <varname>errno</varname>.) </para> <para> <varname>sqlca</varname> covers both warnings and errors. If multiple warnings or errors occur during the execution of a statement, then <varname>sqlca</varname> will only contain information about the last one. </para> <para> If no error occurred in the last <acronym>SQL</acronym> statement, <literal>sqlca.sqlcode</literal> will be 0 and <literal>sqlca.sqlstate</literal> will be <literal>"00000"</literal>. If a warning or error occurred, then <literal>sqlca.sqlcode</literal> will be negative and <literal>sqlca.sqlstate</literal> will be different from <literal>"00000"</literal>. A positive <literal>sqlca.sqlcode</literal> indicates a harmless condition, such as that the last query returned zero rows. <literal>sqlcode</literal> and <literal>sqlstate</literal> are two different error code schemes; details appear below. </para> <para> If the last SQL statement was successful, then <literal>sqlca.sqlerrd[1]</literal> contains the OID of the processed row, if applicable, and <literal>sqlca.sqlerrd[2]</literal> contains the number of processed or returned rows, if applicable to the command. </para> <para> In case of an error or warning, <literal>sqlca.sqlerrm.sqlerrmc</literal> will contain a string that describes the error. The field <literal>sqlca.sqlerrm.sqlerrml</literal> contains the length of the error message that is stored in <literal>sqlca.sqlerrm.sqlerrmc</literal> (the result of <function>strlen()</function>, not really interesting for a C programmer). </para> <para> In case of a warning, <literal>sqlca.sqlwarn[2]</literal> is set to <literal>W</literal>. (In all other cases, it is set to something different from <literal>W</literal>.) If <literal>sqlca.sqlwarn[1]</literal> is set to <literal>W</literal>, then a value was truncated when it was stored in a host variable. <literal>sqlca.sqlwarn[0]</literal> is set to <literal>W</literal> if any of the other elements are set to indicate a warning. </para> <para> The fields <structfield>sqlcaid</structfield>, <structfield>sqlcabc</structfield>, <structfield>sqlerrp</structfield>, and the remaining elements of <structfield>sqlerrd</structfield> and <structfield>sqlwarn</structfield> currently contain no useful information. </para> <para> The structure <varname>sqlca</varname> is not defined in the SQL standard, but is implemented in several other SQL database systems. The definitions are similar in the core, but if you want to write portable applications, then you should investigate the different implementations carefully. </para> </sect2> <sect2> <title><literal>SQLSTATE</literal> vs <literal>SQLCODE</literal></title> <para> The fields <literal>sqlca.sqlstate</literal> and <literal>sqlca.sqlcode</literal> are two different schemes that provide error codes. Both are specified in the SQL standard, but <literal>SQLCODE</literal> has been marked deprecated in the 1992 edition of the standard and has been dropped in the 1999 edition. Therefore, new applications are strongly encouraged to use <literal>SQLSTATE</literal>. </para> <para> <literal>SQLSTATE</literal> is a five-character array. The five characters contain digits or upper-case letters that represent codes of various error and warning conditions. <literal>SQLSTATE</literal> has a hierarchical scheme: the first two characters indicate the general class of the condition, the last three characters indicate a subclass of the general condition. A successful state is indicated by the code <literal>00000</literal>. The <literal>SQLSTATE</literal> codes are for the most part defined in the SQL standard. The <productname>PostgreSQL</productname> server natively supports <literal>SQLSTATE</literal> error codes; therefore a high degree of consistency can be achieved by using this error code scheme throughout all applications. For further information see <xref linkend="errcodes-appendix">. </para> <para> <literal>SQLCODE</literal>, the deprecated error code scheme, is a simple integer. A value of 0 indicates success, a positive value indicates success with additional information, a negative value indicates an error. The SQL standard only defines the positive value +100, which indicates that the last command returned or affected zero rows, and no specific negative values. Therefore, this scheme can only achieve poor portability and does not have a hierarchical code assignment. Historically, the embedded SQL processor for <productname>PostgreSQL</productname> has assigned some specific <literal>SQLCODE</literal> values for its use, which are listed below with their numeric value and their symbolic name. Remember that these are not portable to other SQL implementations. To simplify the porting of applications to the <literal>SQLSTATE</literal> scheme, the corresponding <literal>SQLSTATE</literal> is also listed. There is, however, no one-to-one or one-to-many mapping between the two schemes (indeed it is many-to-many), so you should consult the global <literal>SQLSTATE</literal> listing in <xref linkend="errcodes-appendix"> in each case. </para> <para> These are the assigned <literal>SQLCODE</literal> values: <variablelist> <varlistentry> <term>-12 (<symbol>ECPG_OUT_OF_MEMORY</symbol>)</term> <listitem> <para> Indicates that your virtual memory is exhausted. (SQLSTATE YE001) </para> </listitem> </varlistentry> <varlistentry> <term>-200 (<symbol>ECPG_UNSUPPORTED</symbol>)</term> <listitem> <para> Indicates the preprocessor has generated something that the library does not know about. Perhaps you are running incompatible versions of the preprocessor and the library. (SQLSTATE YE002) </para> </listitem> </varlistentry> <varlistentry> <term>-201 (<symbol>ECPG_TOO_MANY_ARGUMENTS</symbol>)</term> <listitem> <para> This means that the command specified more host variables than the command expected. (SQLSTATE 07001 or 07002) </para> </listitem> </varlistentry> <varlistentry> <term>-202 (<symbol>ECPG_TOO_FEW_ARGUMENTS</symbol>)</term> <listitem> <para> This means that the command specified fewer host variables than the command expected. (SQLSTATE 07001 or 07002) </para> </listitem> </varlistentry> <varlistentry> <term>-203 (<symbol>ECPG_TOO_MANY_MATCHES</symbol>)</term> <listitem> <para> This means a query has returned multiple rows but the statement was only prepared to store one result row (for example, because the specified variables are not arrays). (SQLSTATE 21000) </para> </listitem> </varlistentry> <varlistentry> <term>-204 (<symbol>ECPG_INT_FORMAT</symbol>)</term> <listitem> <para> The host variable is of type <type>int</type> and the datum in the database is of a different type and contains a value that cannot be interpreted as an <type>int</type>. The library uses <function>strtol()</function> for this conversion. (SQLSTATE 42804) </para> </listitem> </varlistentry> <varlistentry> <term>-205 (<symbol>ECPG_UINT_FORMAT</symbol>)</term> <listitem> <para> The host variable is of type <type>unsigned int</type> and the datum in the database is of a different type and contains a value that cannot be interpreted as an <type>unsigned int</type>. The library uses <function>strtoul()</function> for this conversion. (SQLSTATE 42804) </para> </listitem> </varlistentry> <varlistentry> <term>-206 (<symbol>ECPG_FLOAT_FORMAT</symbol>)</term> <listitem> <para> The host variable is of type <type>float</type> and the datum in the database is of another type and contains a value that cannot be interpreted as a <type>float</type>. The library uses <function>strtod()</function> for this conversion. (SQLSTATE 42804) </para> </listitem> </varlistentry> <varlistentry> <term>-207 (<symbol>ECPG_CONVERT_BOOL</symbol>)</term> <listitem> <para> This means the host variable is of type <type>bool</type> and the datum in the database is neither <literal>'t'</> nor <literal>'f'</>. (SQLSTATE 42804) </para> </listitem> </varlistentry> <varlistentry> <term>-208 (<symbol>ECPG_EMPTY</symbol>)</term> <listitem> <para> The statement sent to the <productname>PostgreSQL</productname> server was empty. (This cannot normally happen in an embedded SQL program, so it may point to an internal error.) (SQLSTATE YE002) </para> </listitem> </varlistentry> <varlistentry> <term>-209 (<symbol>ECPG_MISSING_INDICATOR</symbol>)</term> <listitem> <para> A null value was returned and no null indicator variable was supplied. (SQLSTATE 22002) </para> </listitem> </varlistentry> <varlistentry> <term>-210 (<symbol>ECPG_NO_ARRAY</symbol>)</term> <listitem> <para> An ordinary variable was used in a place that requires an array. (SQLSTATE 42804) </para> </listitem> </varlistentry> <varlistentry> <term>-211 (<symbol>ECPG_DATA_NOT_ARRAY</symbol>)</term> <listitem> <para> The database returned an ordinary variable in a place that requires array value. (SQLSTATE 42804) </para> </listitem> </varlistentry> <varlistentry> <term>-220 (<symbol>ECPG_NO_CONN</symbol>)</term> <listitem> <para> The program tried to access a connection that does not exist. (SQLSTATE 08003) </para> </listitem> </varlistentry> <varlistentry> <term>-221 (<symbol>ECPG_NOT_CONN</symbol>)</term> <listitem> <para> The program tried to access a connection that does exist but is not open. (This is an internal error.) (SQLSTATE YE002) </para> </listitem> </varlistentry> <varlistentry> <term>-230 (<symbol>ECPG_INVALID_STMT</symbol>)</term> <listitem> <para> The statement you are trying to use has not been prepared. (SQLSTATE 26000) </para> </listitem> </varlistentry> <varlistentry> <term>-240 (<symbol>ECPG_UNKNOWN_DESCRIPTOR</symbol>)</term> <listitem> <para> The descriptor specified was not found. The statement you are trying to use has not been prepared. (SQLSTATE 33000) </para> </listitem> </varlistentry> <varlistentry> <term>-241 (<symbol>ECPG_INVALID_DESCRIPTOR_INDEX</symbol>)</term> <listitem> <para> The descriptor index specified was out of range. (SQLSTATE 07009) </para> </listitem> </varlistentry> <varlistentry> <term>-242 (<symbol>ECPG_UNKNOWN_DESCRIPTOR_ITEM</symbol>)</term> <listitem> <para> An invalid descriptor item was requested. (This is an internal error.) (SQLSTATE YE002) </para> </listitem> </varlistentry>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?