gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 line
5.6KB

  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.12. Large Objects</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-library.html" title="35.11. Library Functions" /><link rel="next" href="ecpg-cpp.html" title="35.13. C++ Applications" /></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.12. Large Objects</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-library.html" title="35.11. Library Functions">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-cpp.html" title="35.13. C++ Applications">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="ECPG-LO"><div class="titlepage"><div><div><h2 class="title" style="clear: both">35.12. Large Objects</h2></div></div></div><p>
  3. Large objects are not directly supported by ECPG, but ECPG
  4. application can manipulate large objects through the libpq large
  5. object functions, obtaining the necessary <code class="type">PGconn</code>
  6. object by calling the <code class="function">ECPGget_PGconn()</code>
  7. function. (However, use of
  8. the <code class="function">ECPGget_PGconn()</code> function and touching
  9. <code class="type">PGconn</code> objects directly should be done very carefully
  10. and ideally not mixed with other ECPG database access calls.)
  11. </p><p>
  12. For more details about the <code class="function">ECPGget_PGconn()</code>, see
  13. <a class="xref" href="ecpg-library.html" title="35.11. Library Functions">Section 35.11</a>. For information about the large
  14. object function interface, see <a class="xref" href="largeobjects.html" title="Chapter 34. Large Objects">Chapter 34</a>.
  15. </p><p>
  16. Large object functions have to be called in a transaction block, so
  17. when autocommit is off, <code class="command">BEGIN</code> commands have to
  18. be issued explicitly.
  19. </p><p>
  20. <a class="xref" href="ecpg-lo.html#ECPG-LO-EXAMPLE" title="Example 35.2. ECPG Program Accessing Large Objects">Example 35.2</a> shows an example program that
  21. illustrates how to create, write, and read a large object in an
  22. ECPG application.
  23. </p><div class="example" id="ECPG-LO-EXAMPLE"><p class="title"><strong>Example 35.2. ECPG Program Accessing Large Objects</strong></p><div class="example-contents"><pre class="programlisting">
  24. #include &lt;stdio.h&gt;
  25. #include &lt;stdlib.h&gt;
  26. #include &lt;libpq-fe.h&gt;
  27. #include &lt;libpq/libpq-fs.h&gt;
  28. EXEC SQL WHENEVER SQLERROR STOP;
  29. int
  30. main(void)
  31. {
  32. PGconn *conn;
  33. Oid loid;
  34. int fd;
  35. char buf[256];
  36. int buflen = 256;
  37. char buf2[256];
  38. int rc;
  39. memset(buf, 1, buflen);
  40. EXEC SQL CONNECT TO testdb AS con1;
  41. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  42. conn = ECPGget_PGconn("con1");
  43. printf("conn = %p\n", conn);
  44. /* create */
  45. loid = lo_create(conn, 0);
  46. if (loid &amp;lt; 0)
  47. printf("lo_create() failed: %s", PQerrorMessage(conn));
  48. printf("loid = %d\n", loid);
  49. /* write test */
  50. fd = lo_open(conn, loid, INV_READ|INV_WRITE);
  51. if (fd &amp;lt; 0)
  52. printf("lo_open() failed: %s", PQerrorMessage(conn));
  53. printf("fd = %d\n", fd);
  54. rc = lo_write(conn, fd, buf, buflen);
  55. if (rc &amp;lt; 0)
  56. printf("lo_write() failed\n");
  57. rc = lo_close(conn, fd);
  58. if (rc &amp;lt; 0)
  59. printf("lo_close() failed: %s", PQerrorMessage(conn));
  60. /* read test */
  61. fd = lo_open(conn, loid, INV_READ);
  62. if (fd &amp;lt; 0)
  63. printf("lo_open() failed: %s", PQerrorMessage(conn));
  64. printf("fd = %d\n", fd);
  65. rc = lo_read(conn, fd, buf2, buflen);
  66. if (rc &amp;lt; 0)
  67. printf("lo_read() failed\n");
  68. rc = lo_close(conn, fd);
  69. if (rc &amp;lt; 0)
  70. printf("lo_close() failed: %s", PQerrorMessage(conn));
  71. /* check */
  72. rc = memcmp(buf, buf2, buflen);
  73. printf("memcmp() = %d\n", rc);
  74. /* cleanup */
  75. rc = lo_unlink(conn, loid);
  76. if (rc &amp;lt; 0)
  77. printf("lo_unlink() failed: %s", PQerrorMessage(conn));
  78. EXEC SQL COMMIT;
  79. EXEC SQL DISCONNECT ALL;
  80. return 0;
  81. }
  82. </pre></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-library.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-cpp.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">35.11. Library Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 35.13. <acronym class="acronym">C++</acronym> Applications</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1