File: advanced.html

package info (click to toggle)
pygresql 1%3A5.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,340 kB
  • ctags: 2,187
  • sloc: python: 13,239; ansic: 4,975; makefile: 164
file content (316 lines) | stat: -rw-r--r-- 18,605 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316


<!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>Examples for advanced features &mdash; PyGreSQL 5.0 documentation</title>
    
    <link rel="stylesheet" href="../../_static/cloud.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygresql.css" type="text/css" />
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Noticia+Text|Open+Sans|Droid+Sans+Mono" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '5.0.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/jquery.cookie.js"></script>
    <script type="text/javascript" src="../../_static/cloud.js"></script>
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
    <link rel="copyright" title="Copyright" href="../../copyright.html" />
    <link rel="top" title="PyGreSQL 5.0 documentation" href="../index.html" />
    <link rel="up" title="A PostgreSQL Primer" href="index.html" />
    <link rel="next" title="Examples for using SQL functions" href="func.html" />
    <link rel="prev" title="Basic examples" href="basic.html" /> 
        <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body role="document">
<div class="pageheader related" role="navigation" aria-label="related navigation">
  <ul>
    <li><a href="../../index.html">Home</a></li>
    <li><a href="../../download/index.html">Download</a></li>
    <li><a href="../index.html">Documentation</a></li>
    <li><a href="../../community/index.html">Community</a></li>
  </ul>
  <div class="logo">
    <a href="../../index.html">PyGreSQL</a>
  </div>
</div>

</div>

    <div class="relbar-top">
        
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> &nbsp; &nbsp;</li>
        <li class="right" >
          <a href="func.html" title="Examples for using SQL functions"
             accesskey="N">next</a> &nbsp; &nbsp;</li>
        <li class="right" >
          <a href="basic.html" title="Basic examples"
             accesskey="P">previous</a> &nbsp; &nbsp;</li>
    <li><a href="../index.html">PyGreSQL 5.0 documentation</a> &raquo;</li>

          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">A PostgreSQL Primer</a> &raquo;</li> 
      </ul>
    </div>
    </div>

  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="examples-for-advanced-features">
<h1>Examples for advanced features<a class="headerlink" href="#examples-for-advanced-features" title="Permalink to this headline">¶</a></h1>
<p>In this section, we show how to use some advanced features of PostgreSQL
using the classic PyGreSQL interface.</p>
<p>We assume that you have already created a connection to the PostgreSQL
database, as explained in the <a class="reference internal" href="basic.html"><span class="doc">Basic examples</span></a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pg</span> <span class="k">import</span> <span class="n">DB</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">db</span> <span class="o">=</span> <span class="n">DB</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">query</span> <span class="o">=</span> <span class="n">query</span>
</pre></div>
</div>
<div class="section" id="inheritance">
<h2>Inheritance<a class="headerlink" href="#inheritance" title="Permalink to this headline">¶</a></h2>
<p>A table can inherit from zero or more tables. A query can reference either
all rows of a table or all rows of a table plus all of its descendants.</p>
<p>For example, the capitals table inherits from cities table (it inherits
all data fields from cities):</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;cities&#39;</span><span class="p">,</span> <span class="p">[</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;San Francisco&#39;, 7.24E+5, 63&quot;</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;Las Vegas&#39;, 2.583E+5, 2174&quot;</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;Mariposa&#39;, 1200, 1953&quot;</span><span class="p">]),</span>
<span class="gp">... </span>    <span class="p">(</span><span class="s">&#39;capitals&#39;</span><span class="p">,</span> <span class="p">[</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;Sacramento&#39;,3.694E+5,30,&#39;CA&#39;&quot;</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;Madison&#39;, 1.913E+5, 845, &#39;WI&#39;&quot;</span><span class="p">])]</span>
</pre></div>
</div>
<p>Now, let&#8217;s populate the tables:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;cities&#39;</span><span class="p">,</span> <span class="p">[</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;San Francisco&#39;, 7.24E+5, 63&quot;</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;Las Vegas&#39;, 2.583E+5, 2174&quot;</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;Mariposa&#39;, 1200, 1953&quot;</span><span class="p">],</span>
<span class="gp">... </span>    <span class="s">&#39;capitals&#39;</span><span class="p">,</span> <span class="p">[</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;Sacramento&#39;,3.694E+5,30,&#39;CA&#39;&quot;</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">&quot;&#39;Madison&#39;, 1.913E+5, 845, &#39;WI&#39;&quot;</span><span class="p">]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">table</span><span class="p">,</span> <span class="n">rows</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">rows</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">query</span><span class="p">(</span><span class="s">&quot;INSERT INTO %s VALUES (%s)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">row</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">query</span><span class="p">(</span><span class="s">&quot;SELECT * FROM cities&quot;</span><span class="p">))</span>
<span class="go">    name     |population|altitude</span>
<span class="go">-------------+----------+--------</span>
<span class="go">San Francisco|    724000|      63</span>
<span class="go">Las Vegas    |    258300|    2174</span>
<span class="go">Mariposa     |      1200|    1953</span>
<span class="go">Sacramento   |    369400|      30</span>
<span class="go">Madison      |    191300|     845</span>
<span class="go">(5 rows)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">query</span><span class="p">(</span><span class="s">&quot;SELECT * FROM capitals&quot;</span><span class="p">))</span>
<span class="go">   name   |population|altitude|state</span>
<span class="go">----------+----------+--------+-----</span>
<span class="go">Sacramento|    369400|      30|CA</span>
<span class="go">Madison   |    191300|     845|WI</span>
<span class="go">(2 rows)</span>
</pre></div>
</div>
<p>You can find all cities, including capitals, that are located at an altitude
of 500 feet or higher by:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">query</span><span class="p">(</span><span class="s">&quot;&quot;&quot;SELECT c.name, c.altitude</span>
<span class="gp">... </span><span class="s">    FROM cities</span>
<span class="gp">... </span><span class="s">    WHERE altitude &gt; 500&quot;&quot;&quot;</span><span class="p">))</span>
<span class="go">  name   |altitude</span>
<span class="go">---------+--------</span>
<span class="go">Las Vegas|    2174</span>
<span class="go">Mariposa |    1953</span>
<span class="go">Madison  |     845</span>
<span class="go">(3 rows)</span>
</pre></div>
</div>
<p>On the other hand, the following query references rows of the base table only,
i.e. it finds all cities that are not state capitals and are situated at an
altitude of 500 feet or higher:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">query</span><span class="p">(</span><span class="s">&quot;&quot;&quot;SELECT name, altitude</span>
<span class="gp">... </span><span class="s">    FROM ONLY cities</span>
<span class="gp">... </span><span class="s">    WHERE altitude &gt; 500&quot;&quot;&quot;</span><span class="p">))</span>
<span class="go">  name   |altitude</span>
<span class="go">---------+--------</span>
<span class="go">Las Vegas|    2174</span>
<span class="go">Mariposa |    1953</span>
<span class="go">(2 rows)</span>
</pre></div>
</div>
</div>
<div class="section" id="arrays">
<h2>Arrays<a class="headerlink" href="#arrays" title="Permalink to this headline">¶</a></h2>
<p>Attributes can be arrays of base types or user-defined types:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span><span class="p">(</span><span class="s">&quot;&quot;&quot;CREATE TABLE sal_emp (</span>
<span class="gp">... </span><span class="s">       name                  text,</span>
<span class="gp">... </span><span class="s">       pay_by_quarter        int4[],</span>
<span class="gp">... </span><span class="s">       pay_by_extra_quarter  int8[],</span>
<span class="gp">... </span><span class="s">       schedule              text[][])&quot;&quot;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Insert instances with array attributes. Note the use of braces:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span><span class="p">(</span><span class="s">&quot;&quot;&quot;INSERT INTO sal_emp VALUES (</span>
<span class="gp">... </span><span class="s">    &#39;Bill&#39;, &#39;{10000,10000,10000,10000}&#39;,</span>
<span class="gp">... </span><span class="s">    &#39;{9223372036854775800,9223372036854775800,9223372036854775800}&#39;,</span>
<span class="gp">... </span><span class="s">    &#39;{{&quot;meeting&quot;, &quot;lunch&quot;}, {&quot;training&quot;, &quot;presentation&quot;}}&#39;)&quot;&quot;&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">query</span><span class="p">(</span><span class="s">&quot;&quot;&quot;INSERT INTO sal_emp VALUES (</span>
<span class="gp">... </span><span class="s">    &#39;Carol&#39;, &#39;{20000,25000,25000,25000}&#39;,</span>
<span class="gp">... </span><span class="s">     &#39;{9223372036854775807,9223372036854775807,9223372036854775807}&#39;,</span>
<span class="gp">... </span><span class="s">     &#39;{{&quot;breakfast&quot;, &quot;consulting&quot;}, {&quot;meeting&quot;, &quot;lunch&quot;}}&#39;)&quot;&quot;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Queries on array attributes:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span><span class="p">(</span><span class="s">&quot;&quot;&quot;SELECT name FROM sal_emp WHERE</span>
<span class="gp">... </span><span class="s">    sal_emp.pay_by_quarter[1] != sal_emp.pay_by_quarter[2]&quot;&quot;&quot;</span><span class="p">)</span>
<span class="go">name</span>
<span class="go">-----</span>
<span class="go">Carol</span>
<span class="go">(1 row)</span>
</pre></div>
</div>
<p>Retrieve third quarter pay of all employees:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span><span class="p">(</span><span class="s">&quot;SELECT sal_emp.pay_by_quarter[3] FROM sal_emp&quot;</span><span class="p">)</span>
<span class="go">pay_by_quarter</span>
<span class="go">--------------</span>
<span class="go">         10000</span>
<span class="go">         25000</span>
<span class="go">(2 rows)</span>
</pre></div>
</div>
<p>Retrieve third quarter extra pay of all employees:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span><span class="p">(</span><span class="s">&quot;SELECT sal_emp.pay_by_extra_quarter[3] FROM sal_emp&quot;</span><span class="p">)</span>
<span class="go">pay_by_extra_quarter</span>
<span class="go">--------------------</span>
<span class="go"> 9223372036854775800</span>
<span class="go"> 9223372036854775807</span>
<span class="go">(2 rows)</span>
</pre></div>
</div>
<p>Retrieve first two quarters of extra quarter pay of all employees:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span><span class="p">(</span><span class="s">&quot;SELECT sal_emp.pay_by_extra_quarter[1:2] FROM sal_emp&quot;</span><span class="p">)</span>
<span class="go">          pay_by_extra_quarter</span>
<span class="go">-----------------------------------------</span>
<span class="go">{9223372036854775800,9223372036854775800}</span>
<span class="go">{9223372036854775807,9223372036854775807}</span>
<span class="go">(2 rows)</span>
</pre></div>
</div>
<p>Select subarrays:</p>
<div class="highlight-default"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span><span class="p">(</span><span class="s">&quot;&quot;&quot;SELECT sal_emp.schedule[1:2][1:1] FROM sal_emp</span>
<span class="gp">... </span><span class="s">    WHERE sal_emp.name = &#39;Bill&#39;&quot;&quot;&quot;</span><span class="p">)</span>
<span class="go">       schedule</span>
<span class="go">----------------------</span>
<span class="go">{{meeting},{training}}</span>
<span class="go">(1 row)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
        <p class="logo"><a href="../index.html" title="contents/index">
          <img class="logo" src="../../_static/pygresql.png" alt="Logo"/>
        </a></p><div class="sphinxlocaltoc">
    <h3><a href="../../index.html">Page contents</a></h3>
    <ul>
<li><a class="reference internal" href="#">Examples for advanced features</a><ul>
<li><a class="reference internal" href="#inheritance">Inheritance</a></li>
<li><a class="reference internal" href="#arrays">Arrays</a></li>
</ul>
</li>
</ul>

  </div>
  <div class="sphinxprev">
    <h4>Previous page</h4>
    <p class="topless"><a href="basic.html"
                          title="Previous page">&larr; Basic examples</a></p>
  </div>
  <div class="sphinxnext">
    <h4>Next page</h4>
    <p class="topless"><a href="func.html"
                          title="Next page">&rarr; Examples for using SQL functions</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/contents/postgres/advanced.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="relbar-bottom">
        
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> &nbsp; &nbsp;</li>
        <li class="right" >
          <a href="func.html" title="Examples for using SQL functions"
             >next</a> &nbsp; &nbsp;</li>
        <li class="right" >
          <a href="basic.html" title="Basic examples"
             >previous</a> &nbsp; &nbsp;</li>
    <li><a href="../index.html">PyGreSQL 5.0 documentation</a> &raquo;</li>

          <li class="nav-item nav-item-1"><a href="index.html" >A PostgreSQL Primer</a> &raquo;</li> 
      </ul>
    </div>
    </div>

    <div class="footer" role="contentinfo">
        &copy; <a href="../../copyright.html">Copyright</a> 2016, The PyGreSQL team.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.1.
    </div>
    <!-- cloud_sptheme 1.4 -->
  </body>
</html>