gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

198 lignes
13KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>35.2. Managing Database Connections</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="ecpg-concept.html" title="35.1. The Concept" /><link rel="next" href="ecpg-commands.html" title="35.3. Running SQL Commands" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">35.2. Managing Database Connections</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-concept.html" title="35.1. The Concept">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg.html" title="Chapter 35. ECPG - Embedded SQL in C">Up</a></td><th width="60%" align="center">Chapter 35. <span xmlns="http://www.w3.org/1999/xhtml" class="application">ECPG</span> - Embedded <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> in C</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-commands.html" title="35.3. Running SQL Commands">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="ECPG-CONNECT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">35.2. Managing Database Connections</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="ecpg-connect.html#ECPG-CONNECTING">35.2.1. Connecting to the Database Server</a></span></dt><dt><span class="sect2"><a href="ecpg-connect.html#ECPG-SET-CONNECTION">35.2.2. Choosing a Connection</a></span></dt><dt><span class="sect2"><a href="ecpg-connect.html#ECPG-DISCONNECT">35.2.3. Closing a Connection</a></span></dt></dl></div><p>
  3. This section describes how to open, close, and switch database
  4. connections.
  5. </p><div class="sect2" id="ECPG-CONNECTING"><div class="titlepage"><div><div><h3 class="title">35.2.1. Connecting to the Database Server</h3></div></div></div><p>
  6. One connects to a database using the following statement:
  7. </p><pre class="programlisting">
  8. EXEC SQL CONNECT TO <em class="replaceable"><code>target</code></em> [<span class="optional">AS <em class="replaceable"><code>connection-name</code></em></span>] [<span class="optional">USER <em class="replaceable"><code>user-name</code></em></span>];
  9. </pre><p>
  10. The <em class="replaceable"><code>target</code></em> can be specified in the
  11. following ways:
  12. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
  13. <code class="literal"><em class="replaceable"><code>dbname</code></em>[<span class="optional">@<em class="replaceable"><code>hostname</code></em></span>][<span class="optional">:<em class="replaceable"><code>port</code></em></span>]</code>
  14. </li><li class="listitem">
  15. <code class="literal">tcp:postgresql://<em class="replaceable"><code>hostname</code></em>[<span class="optional">:<em class="replaceable"><code>port</code></em></span>][<span class="optional">/<em class="replaceable"><code>dbname</code></em></span>][<span class="optional">?<em class="replaceable"><code>options</code></em></span>]</code>
  16. </li><li class="listitem">
  17. <code class="literal">unix:postgresql://<em class="replaceable"><code>hostname</code></em>[<span class="optional">:<em class="replaceable"><code>port</code></em></span>][<span class="optional">/<em class="replaceable"><code>dbname</code></em></span>][<span class="optional">?<em class="replaceable"><code>options</code></em></span>]</code>
  18. </li><li class="listitem">
  19. an SQL string literal containing one of the above forms
  20. </li><li class="listitem">
  21. a reference to a character variable containing one of the above forms (see examples)
  22. </li><li class="listitem">
  23. <code class="literal">DEFAULT</code>
  24. </li></ul></div><p>
  25. If you specify the connection target literally (that is, not
  26. through a variable reference) and you don't quote the value, then
  27. the case-insensitivity rules of normal SQL are applied. In that
  28. case you can also double-quote the individual parameters separately
  29. as needed. In practice, it is probably less error-prone to use a
  30. (single-quoted) string literal or a variable reference. The
  31. connection target <code class="literal">DEFAULT</code> initiates a connection
  32. to the default database under the default user name. No separate
  33. user name or connection name can be specified in that case.
  34. </p><p>
  35. There are also different ways to specify the user name:
  36. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
  37. <code class="literal"><em class="replaceable"><code>username</code></em></code>
  38. </li><li class="listitem">
  39. <code class="literal"><em class="replaceable"><code>username</code></em>/<em class="replaceable"><code>password</code></em></code>
  40. </li><li class="listitem">
  41. <code class="literal"><em class="replaceable"><code>username</code></em> IDENTIFIED BY <em class="replaceable"><code>password</code></em></code>
  42. </li><li class="listitem">
  43. <code class="literal"><em class="replaceable"><code>username</code></em> USING <em class="replaceable"><code>password</code></em></code>
  44. </li></ul></div><p>
  45. As above, the parameters <em class="replaceable"><code>username</code></em> and
  46. <em class="replaceable"><code>password</code></em> can be an SQL identifier, an
  47. SQL string literal, or a reference to a character variable.
  48. </p><p>
  49. If the connection target includes any <em class="replaceable"><code>options</code></em>,
  50. those consist of
  51. <code class="literal"><em class="replaceable"><code>keyword</code></em>=<em class="replaceable"><code>value</code></em></code>
  52. specifications separated by ampersands (<code class="literal">&amp;</code>).
  53. The allowed key words are the same ones recognized
  54. by <span class="application">libpq</span> (see
  55. <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="33.1.2. Parameter Key Words">Section 33.1.2</a>). Spaces are ignored before
  56. any <em class="replaceable"><code>keyword</code></em> or <em class="replaceable"><code>value</code></em>,
  57. though not within or after one. Note that there is no way to
  58. write <code class="literal">&amp;</code> within a <em class="replaceable"><code>value</code></em>.
  59. </p><p>
  60. The <em class="replaceable"><code>connection-name</code></em> is used to handle
  61. multiple connections in one program. It can be omitted if a
  62. program uses only one connection. The most recently opened
  63. connection becomes the current connection, which is used by default
  64. when an SQL statement is to be executed (see later in this
  65. chapter).
  66. </p><p>
  67. If untrusted users have access to a database that has not adopted a
  68. <a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.9.6. Usage Patterns">secure schema usage pattern</a>,
  69. begin each session by removing publicly-writable schemas
  70. from <code class="varname">search_path</code>. For example,
  71. add <code class="literal">options=-c search_path=</code>
  72. to <code class="literal"><em class="replaceable"><code>options</code></em></code>, or
  73. issue <code class="literal">EXEC SQL SELECT pg_catalog.set_config('search_path', '',
  74. false);</code> after connecting. This consideration is not specific to
  75. ECPG; it applies to every interface for executing arbitrary SQL commands.
  76. </p><p>
  77. Here are some examples of <code class="command">CONNECT</code> statements:
  78. </p><pre class="programlisting">
  79. EXEC SQL CONNECT TO mydb@sql.mydomain.com;
  80. EXEC SQL CONNECT TO unix:postgresql://sql.mydomain.com/mydb AS myconnection USER john;
  81. EXEC SQL BEGIN DECLARE SECTION;
  82. const char *target = "mydb@sql.mydomain.com";
  83. const char *user = "john";
  84. const char *passwd = "secret";
  85. EXEC SQL END DECLARE SECTION;
  86. ...
  87. EXEC SQL CONNECT TO :target USER :user USING :passwd;
  88. /* or EXEC SQL CONNECT TO :target USER :user/:passwd; */
  89. </pre><p>
  90. The last form makes use of the variant referred to above as
  91. character variable reference. You will see in later sections how C
  92. variables can be used in SQL statements when you prefix them with a
  93. colon.
  94. </p><p>
  95. Be advised that the format of the connection target is not
  96. specified in the SQL standard. So if you want to develop portable
  97. applications, you might want to use something based on the last
  98. example above to encapsulate the connection target string
  99. somewhere.
  100. </p></div><div class="sect2" id="ECPG-SET-CONNECTION"><div class="titlepage"><div><div><h3 class="title">35.2.2. Choosing a Connection</h3></div></div></div><p>
  101. SQL statements in embedded SQL programs are by default executed on
  102. the current connection, that is, the most recently opened one. If
  103. an application needs to manage multiple connections, then there are
  104. two ways to handle this.
  105. </p><p>
  106. The first option is to explicitly choose a connection for each SQL
  107. statement, for example:
  108. </p><pre class="programlisting">
  109. EXEC SQL AT <em class="replaceable"><code>connection-name</code></em> SELECT ...;
  110. </pre><p>
  111. This option is particularly suitable if the application needs to
  112. use several connections in mixed order.
  113. </p><p>
  114. If your application uses multiple threads of execution, they cannot share a
  115. connection concurrently. You must either explicitly control access to the connection
  116. (using mutexes) or use a connection for each thread.
  117. </p><p>
  118. The second option is to execute a statement to switch the current
  119. connection. That statement is:
  120. </p><pre class="programlisting">
  121. EXEC SQL SET CONNECTION <em class="replaceable"><code>connection-name</code></em>;
  122. </pre><p>
  123. This option is particularly convenient if many statements are to be
  124. executed on the same connection.
  125. </p><p>
  126. Here is an example program managing multiple database connections:
  127. </p><pre class="programlisting">
  128. #include &lt;stdio.h&gt;
  129. EXEC SQL BEGIN DECLARE SECTION;
  130. char dbname[1024];
  131. EXEC SQL END DECLARE SECTION;
  132. int
  133. main()
  134. {
  135. EXEC SQL CONNECT TO testdb1 AS con1 USER testuser;
  136. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  137. EXEC SQL CONNECT TO testdb2 AS con2 USER testuser;
  138. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  139. EXEC SQL CONNECT TO testdb3 AS con3 USER testuser;
  140. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  141. /* This query would be executed in the last opened database "testdb3". */
  142. EXEC SQL SELECT current_database() INTO :dbname;
  143. printf("current=%s (should be testdb3)\n", dbname);
  144. /* Using "AT" to run a query in "testdb2" */
  145. EXEC SQL AT con2 SELECT current_database() INTO :dbname;
  146. printf("current=%s (should be testdb2)\n", dbname);
  147. /* Switch the current connection to "testdb1". */
  148. EXEC SQL SET CONNECTION con1;
  149. EXEC SQL SELECT current_database() INTO :dbname;
  150. printf("current=%s (should be testdb1)\n", dbname);
  151. EXEC SQL DISCONNECT ALL;
  152. return 0;
  153. }
  154. </pre><p>
  155. This example would produce this output:
  156. </p><pre class="screen">
  157. current=testdb3 (should be testdb3)
  158. current=testdb2 (should be testdb2)
  159. current=testdb1 (should be testdb1)
  160. </pre><p>
  161. </p></div><div class="sect2" id="ECPG-DISCONNECT"><div class="titlepage"><div><div><h3 class="title">35.2.3. Closing a Connection</h3></div></div></div><p>
  162. To close a connection, use the following statement:
  163. </p><pre class="programlisting">
  164. EXEC SQL DISCONNECT [<span class="optional"><em class="replaceable"><code>connection</code></em></span>];
  165. </pre><p>
  166. The <em class="replaceable"><code>connection</code></em> can be specified
  167. in the following ways:
  168. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
  169. <code class="literal"><em class="replaceable"><code>connection-name</code></em></code>
  170. </li><li class="listitem">
  171. <code class="literal">DEFAULT</code>
  172. </li><li class="listitem">
  173. <code class="literal">CURRENT</code>
  174. </li><li class="listitem">
  175. <code class="literal">ALL</code>
  176. </li></ul></div><p>
  177. If no connection name is specified, the current connection is
  178. closed.
  179. </p><p>
  180. It is good style that an application always explicitly disconnect
  181. from every connection it opened.
  182. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-concept.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-commands.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">35.1. The Concept </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 35.3. Running SQL Commands</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1