This file is indexed.

/usr/share/doc/python-psycopg2-doc/html/cursor.html is in python-psycopg2-doc 2.6.1-1build2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
<!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>The cursor class &mdash; Psycopg 2.0 documentation</title>
    
    <link rel="stylesheet" href="_static/psycopg.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '2.0',
        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>
    <link rel="top" title="Psycopg 2.0 documentation" href="index.html" />
    <link rel="next" title="More advanced topics" href="advanced.html" />
    <link rel="prev" title="The connection class" href="connection.html" /> 
  </head>
  <body role="document">
    <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> |</li>
        <li class="right" >
          <a href="advanced.html" title="More advanced topics"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="connection.html" title="The connection class"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Psycopg 2.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="the-cursor-class">
<h1>The <code class="docutils literal"><span class="pre">cursor</span></code> class<a class="headerlink" href="#the-cursor-class" title="Permalink to this headline"></a></h1>
<dl class="class">
<dt id="cursor">
<em class="property">class </em><code class="descname">cursor</code><a class="headerlink" href="#cursor" title="Permalink to this definition"></a></dt>
<dd><p>Allows Python code to execute PostgreSQL command in a database session.
Cursors are created by the <a class="reference internal" href="connection.html#connection.cursor" title="connection.cursor"><code class="xref py py-obj docutils literal"><span class="pre">connection.cursor()</span></code></a> method: they are
bound to the connection for the entire lifetime and all the commands are
executed in the context of the database session wrapped by the connection.</p>
<p>Cursors created from the same connection are not isolated, i.e., any
changes done to the database by a cursor are immediately visible by the
other cursors. Cursors created from different connections can or can not
be isolated, depending on the connections&#8217; <a class="reference internal" href="usage.html#transactions-control"><span>isolation level</span></a>. See also <a class="reference internal" href="connection.html#connection.rollback" title="connection.rollback"><code class="xref py py-obj docutils literal"><span class="pre">rollback()</span></code></a> and
<a class="reference internal" href="connection.html#connection.commit" title="connection.commit"><code class="xref py py-obj docutils literal"><span class="pre">commit()</span></code></a> methods.</p>
<p>Cursors are <em>not</em> thread safe: a multithread application can create
many cursors from the same connection and should use each cursor from
a single thread. See <a class="reference internal" href="usage.html#thread-safety"><span>Thread and process safety</span></a> for details.</p>
<dl class="attribute">
<dt id="cursor.description">
<code class="descname">description</code><a class="headerlink" href="#cursor.description" title="Permalink to this definition"></a></dt>
<dd><p>This read-only attribute is a sequence of 7-item sequences.</p>
<p>Each of these sequences is a named tuple (a regular tuple if
<a class="reference external" href="/usr/share/doc/python3-doc/html/library/collections.html#collections.namedtuple" title="(in Python v3.5)"><code class="xref py py-func docutils literal"><span class="pre">collections.namedtuple()</span></code></a> is not available) containing information
describing one result column:</p>
<ol class="arabic simple" start="0">
<li><code class="xref py py-obj docutils literal"><span class="pre">name</span></code>: the name of the column returned.</li>
<li><code class="xref py py-obj docutils literal"><span class="pre">type_code</span></code>: the PostgreSQL OID of the column. You can use the
<a class="reference external" href="http://www.postgresql.org/docs/current/static/catalog-pg-type.html"><code class="sql docutils literal"><span class="pre">pg_type</span></code></a> system table to get more informations about the type.
This is the value used by Psycopg to decide what Python type use
to represent the value.  See also
<a class="reference internal" href="advanced.html#type-casting-from-sql-to-python"><span>Type casting of SQL types into Python objects</span></a>.</li>
<li><code class="xref py py-obj docutils literal"><span class="pre">display_size</span></code>: the actual length of the column in bytes.
Obtaining this value is computationally intensive, so it is
always <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> unless the <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal"><span class="pre">PSYCOPG_DISPLAY_SIZE</span></code> parameter
is set at compile time. See also <a class="reference external" href="http://www.postgresql.org/docs/current/static/libpq-exec.html#LIBPQ-PQGETLENGTH">PQgetlength</a>.</li>
<li><code class="xref py py-obj docutils literal"><span class="pre">internal_size</span></code>: the size in bytes of the column associated to
this column on the server. Set to a negative value for
variable-size types See also <a class="reference external" href="http://www.postgresql.org/docs/current/static/libpq-exec.html#LIBPQ-PQFSIZE">PQfsize</a>.</li>
<li><code class="xref py py-obj docutils literal"><span class="pre">precision</span></code>: total number of significant digits in columns of
type <a class="reference external" href="http://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL"><code class="sql docutils literal"><span class="pre">NUMERIC</span></code></a>. <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> for other types.</li>
<li><code class="xref py py-obj docutils literal"><span class="pre">scale</span></code>: count of decimal digits in the fractional part in
columns of type <code class="sql docutils literal"><span class="pre">NUMERIC</span></code>. <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> for other types.</li>
<li><code class="xref py py-obj docutils literal"><span class="pre">null_ok</span></code>: always <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> as not easy to retrieve from the libpq.</li>
</ol>
<p>This attribute will be <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> for operations that do not return rows
or if the cursor has not had an operation invoked via the
<a class="reference internal" href="#execute"><code class="xref py py-obj docutils literal"><span class="pre">execute*()</span></code></a> methods yet.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>if possible, columns descriptions are named tuple instead of
regular tuples.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.close">
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.close" title="Permalink to this definition"></a></dt>
<dd><p>Close the cursor now (rather than whenever <code class="xref py py-obj docutils literal"><span class="pre">del</span></code> is executed).
The cursor will be unusable from this point forward; an
<a class="reference internal" href="module.html#psycopg2.InterfaceError" title="psycopg2.InterfaceError"><code class="xref py py-obj docutils literal"><span class="pre">InterfaceError</span></code></a> will be raised if any operation is
attempted with the cursor.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.5: </span>if the cursor is used in a <code class="docutils literal"><span class="pre">with</span></code> statement,
the method is automatically called at the end of the <code class="docutils literal"><span class="pre">with</span></code>
block.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.closed">
<code class="descname">closed</code><a class="headerlink" href="#cursor.closed" title="Permalink to this definition"></a></dt>
<dd><p>Read-only boolean attribute: specifies if the cursor is closed
(<code class="xref py py-obj docutils literal"><span class="pre">True</span></code>) or not (<code class="xref py py-obj docutils literal"><span class="pre">False</span></code>).</p>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.closed" title="cursor.closed"><code class="xref py py-obj docutils literal"><span class="pre">closed</span></code></a> attribute is a Psycopg extension to the
DB API 2.0.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.7.</span></p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.connection">
<code class="descname">connection</code><a class="headerlink" href="#cursor.connection" title="Permalink to this definition"></a></dt>
<dd><p>Read-only attribute returning a reference to the <a class="reference internal" href="connection.html#connection" title="connection"><code class="xref py py-obj docutils literal"><span class="pre">connection</span></code></a>
object on which the cursor was created.</p>
</dd></dl>

<dl class="attribute">
<dt id="cursor.name">
<code class="descname">name</code><a class="headerlink" href="#cursor.name" title="Permalink to this definition"></a></dt>
<dd><p>Read-only attribute containing the name of the cursor if it was
creates as named cursor by <a class="reference internal" href="connection.html#connection.cursor" title="connection.cursor"><code class="xref py py-obj docutils literal"><span class="pre">connection.cursor()</span></code></a>, or <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> if
it is a client side cursor.  See <a class="reference internal" href="usage.html#server-side-cursors"><span>Server side cursors</span></a>.</p>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.name" title="cursor.name"><code class="xref py py-obj docutils literal"><span class="pre">name</span></code></a> attribute is a Psycopg extension to the DB API 2.0.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.scrollable">
<code class="descname">scrollable</code><a class="headerlink" href="#cursor.scrollable" title="Permalink to this definition"></a></dt>
<dd><p>Read/write attribute: specifies if a named cursor is declared
<code class="sql docutils literal"><span class="pre">SCROLL</span></code>, hence is capable to scroll backwards (using
<a class="reference internal" href="#cursor.scroll" title="cursor.scroll"><code class="xref py py-obj docutils literal"><span class="pre">scroll()</span></code></a>). If <code class="xref py py-obj docutils literal"><span class="pre">True</span></code>, the cursor can be scrolled backwards,
if <code class="xref py py-obj docutils literal"><span class="pre">False</span></code> it is never scrollable. If <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> (default) the cursor
scroll option is not specified, usually but not always meaning no
backward scroll (see the <a class="reference external" href="http://www.postgresql.org/docs/current/static/sql-declare.html#SQL-DECLARE-NOTES"><code class="sql docutils literal"><span class="pre">DECLARE</span></code> notes</a>).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">set the value before calling <a class="reference internal" href="#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a> or use the
<a class="reference internal" href="connection.html#connection.cursor" title="connection.cursor"><code class="xref py py-obj docutils literal"><span class="pre">connection.cursor()</span></code></a> <em>scrollable</em> parameter, otherwise the value
will have no effect.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.scrollable" title="cursor.scrollable"><code class="xref py py-obj docutils literal"><span class="pre">scrollable</span></code></a> attribute is a Psycopg extension to the DB API 2.0.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.withhold">
<code class="descname">withhold</code><a class="headerlink" href="#cursor.withhold" title="Permalink to this definition"></a></dt>
<dd><p>Read/write attribute: specifies if a named cursor lifetime should
extend outside of the current transaction, i.e., it is possible to
fetch from the cursor even after a <a class="reference internal" href="connection.html#connection.commit" title="connection.commit"><code class="xref py py-obj docutils literal"><span class="pre">connection.commit()</span></code></a> (but not after
a <a class="reference internal" href="connection.html#connection.rollback" title="connection.rollback"><code class="xref py py-obj docutils literal"><span class="pre">connection.rollback()</span></code></a>).  See <a class="reference internal" href="usage.html#server-side-cursors"><span>Server side cursors</span></a></p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">set the value before calling <a class="reference internal" href="#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a> or use the
<a class="reference internal" href="connection.html#connection.cursor" title="connection.cursor"><code class="xref py py-obj docutils literal"><span class="pre">connection.cursor()</span></code></a> <em>withhold</em> parameter, otherwise the value
will have no effect.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.3.</span></p>
</div>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.withhold" title="cursor.withhold"><code class="xref py py-obj docutils literal"><span class="pre">withhold</span></code></a> attribute is a Psycopg extension to the DB API 2.0.</p>
</div>
</dd></dl>

<p class="rubric" id="execute">Commands execution methods</p>
<dl class="method">
<dt id="cursor.execute">
<code class="descname">execute</code><span class="sig-paren">(</span><em>operation</em><span class="optional">[</span>, <em>parameters</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.execute" title="Permalink to this definition"></a></dt>
<dd><p>Prepare and execute a database operation (query or command).</p>
<p>Parameters may be provided as sequence or mapping and will be bound to
variables in the operation.  Variables are specified either with
positional (<code class="docutils literal"><span class="pre">%s</span></code>) or named (<code class="samp docutils literal"><span class="pre">%(</span><em><span class="pre">name</span></em><span class="pre">)s</span></code>) placeholders. See
<a class="reference internal" href="usage.html#query-parameters"><span>Passing parameters to SQL queries</span></a>.</p>
<p>The method returns <code class="xref py py-obj docutils literal"><span class="pre">None</span></code>. If a query was executed, the returned
values can be retrieved using <a class="reference internal" href="#fetch"><code class="xref py py-obj docutils literal"><span class="pre">fetch*()</span></code></a> methods.</p>
</dd></dl>

<dl class="method">
<dt id="cursor.executemany">
<code class="descname">executemany</code><span class="sig-paren">(</span><em>operation</em>, <em>seq_of_parameters</em><span class="sig-paren">)</span><a class="headerlink" href="#cursor.executemany" title="Permalink to this definition"></a></dt>
<dd><p>Prepare a database operation (query or command) and then execute it
against all parameter tuples or mappings found in the sequence
<code class="xref py py-obj docutils literal"><span class="pre">seq_of_parameters</span></code>.</p>
<p>The function is mostly useful for commands that update the database:
any result set returned by the query is discarded.</p>
<p>Parameters are bounded to the query using the same rules described in
the <a class="reference internal" href="#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a> method.</p>
</dd></dl>

<dl class="method">
<dt id="cursor.callproc">
<code class="descname">callproc</code><span class="sig-paren">(</span><em>procname</em><span class="optional">[</span>, <em>parameters</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.callproc" title="Permalink to this definition"></a></dt>
<dd><p>Call a stored database procedure with the given name. The sequence of
parameters must contain one entry for each argument that the procedure
expects. The result of the call is returned as modified copy of the
input sequence. Input parameters are left untouched, output and
input/output parameters replaced with possibly new values.</p>
<p>The procedure may also provide a result set as output. This must then
be made available through the standard <a class="reference internal" href="#fetch"><code class="xref py py-obj docutils literal"><span class="pre">fetch*()</span></code></a> methods.</p>
</dd></dl>

<dl class="method">
<dt id="cursor.mogrify">
<code class="descname">mogrify</code><span class="sig-paren">(</span><em>operation</em><span class="optional">[</span>, <em>parameters</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.mogrify" title="Permalink to this definition"></a></dt>
<dd><p>Return a query string after arguments binding. The string returned is
exactly the one that would be sent to the database running the
<a class="reference internal" href="#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a> method or similar.</p>
<p>The returned string is always a bytes string.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">mogrify</span><span class="p">(</span><span class="s">&quot;INSERT INTO test (num, data) VALUES (</span><span class="si">%s</span><span class="s">, </span><span class="si">%s</span><span class="s">)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="s">&#39;bar&#39;</span><span class="p">))</span>
<span class="go">&quot;INSERT INTO test (num, data) VALUES (42, E&#39;bar&#39;)&quot;</span>
</pre></div>
</div>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.mogrify" title="cursor.mogrify"><code class="xref py py-obj docutils literal"><span class="pre">mogrify()</span></code></a> method is a Psycopg extension to the DB API 2.0.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.setinputsizes">
<code class="descname">setinputsizes</code><span class="sig-paren">(</span><em>sizes</em><span class="sig-paren">)</span><a class="headerlink" href="#cursor.setinputsizes" title="Permalink to this definition"></a></dt>
<dd><p>This method is exposed in compliance with the DB API 2.0. It currently
does nothing but it is safe to call it.</p>
</dd></dl>

<p class="rubric" id="fetch">Results retrieval methods</p>
<p>The following methods are used to read data from the database after an
<a class="reference internal" href="#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a> call.</p>
<div class="admonition note" id="cursor-iterable">
<p class="first admonition-title">Note</p>
<p><a class="reference internal" href="#cursor" title="cursor"><code class="xref py py-obj docutils literal"><span class="pre">cursor</span></code></a> objects are iterable, so, instead of calling
explicitly <a class="reference internal" href="#cursor.fetchone" title="cursor.fetchone"><code class="xref py py-obj docutils literal"><span class="pre">fetchone()</span></code></a> in a loop, the object itself can
be used:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;SELECT * FROM test;&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">record</span> <span class="ow">in</span> <span class="n">cur</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">record</span>
<span class="gp">...</span>
<span class="go">(1, 100, &quot;abc&#39;def&quot;)</span>
<span class="go">(2, None, &#39;dada&#39;)</span>
<span class="go">(3, 42, &#39;bar&#39;)</span>
</pre></div>
</div>
<div class="last versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>iterating over a <a class="reference internal" href="usage.html#server-side-cursors"><span>named cursor</span></a>
fetches <a class="reference internal" href="#cursor.itersize" title="cursor.itersize"><code class="xref py py-obj docutils literal"><span class="pre">itersize</span></code></a> records at time from the backend.
Previously only one record was fetched per roundtrip, resulting
in a large overhead.</p>
</div>
</div>
<dl class="method">
<dt id="cursor.fetchone">
<code class="descname">fetchone</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.fetchone" title="Permalink to this definition"></a></dt>
<dd><p>Fetch the next row of a query result set, returning a single tuple,
or <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> when no more data is available:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;SELECT * FROM test WHERE id = </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="go">(3, 42, &#39;bar&#39;)</span>
</pre></div>
</div>
<p>A <a class="reference internal" href="module.html#psycopg2.ProgrammingError" title="psycopg2.ProgrammingError"><code class="xref py py-obj docutils literal"><span class="pre">ProgrammingError</span></code></a> is raised if the previous call
to <a class="reference internal" href="#execute"><code class="xref py py-obj docutils literal"><span class="pre">execute*()</span></code></a> did not produce any result set or no call was issued
yet.</p>
</dd></dl>

<dl class="method">
<dt id="cursor.fetchmany">
<code class="descname">fetchmany</code><span class="sig-paren">(</span><span class="optional">[</span><em>size=cursor.arraysize</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.fetchmany" title="Permalink to this definition"></a></dt>
<dd><p>Fetch the next set of rows of a query result, returning a list of
tuples. An empty list is returned when no more rows are available.</p>
<p>The number of rows to fetch per call is specified by the parameter.
If it is not given, the cursor&#8217;s <a class="reference internal" href="#cursor.arraysize" title="cursor.arraysize"><code class="xref py py-obj docutils literal"><span class="pre">arraysize</span></code></a> determines
the number of rows to be fetched. The method should try to fetch as
many rows as indicated by the size parameter. If this is not possible
due to the specified number of rows not being available, fewer rows
may be returned:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;SELECT * FROM test;&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchmany</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">[(1, 100, &quot;abc&#39;def&quot;), (2, None, &#39;dada&#39;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchmany</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">[(3, 42, &#39;bar&#39;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchmany</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>A <a class="reference internal" href="module.html#psycopg2.ProgrammingError" title="psycopg2.ProgrammingError"><code class="xref py py-obj docutils literal"><span class="pre">ProgrammingError</span></code></a> is raised if the previous call to
<a class="reference internal" href="#execute"><code class="xref py py-obj docutils literal"><span class="pre">execute*()</span></code></a> did not produce any result set or no call was issued yet.</p>
<p>Note there are performance considerations involved with the size
parameter.  For optimal performance, it is usually best to use the
<a class="reference internal" href="#cursor.arraysize" title="cursor.arraysize"><code class="xref py py-obj docutils literal"><span class="pre">arraysize</span></code></a> attribute.  If the size parameter is used,
then it is best for it to retain the same value from one
<a class="reference internal" href="#cursor.fetchmany" title="cursor.fetchmany"><code class="xref py py-obj docutils literal"><span class="pre">fetchmany()</span></code></a> call to the next.</p>
</dd></dl>

<dl class="method">
<dt id="cursor.fetchall">
<code class="descname">fetchall</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.fetchall" title="Permalink to this definition"></a></dt>
<dd><p>Fetch all (remaining) rows of a query result, returning them as a list
of tuples.  An empty list is returned if there is no more record to
fetch.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;SELECT * FROM test;&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span>
<span class="go">[(1, 100, &quot;abc&#39;def&quot;), (2, None, &#39;dada&#39;), (3, 42, &#39;bar&#39;)]</span>
</pre></div>
</div>
<p>A <a class="reference internal" href="module.html#psycopg2.ProgrammingError" title="psycopg2.ProgrammingError"><code class="xref py py-obj docutils literal"><span class="pre">ProgrammingError</span></code></a> is raised if the previous call to
<a class="reference internal" href="#execute"><code class="xref py py-obj docutils literal"><span class="pre">execute*()</span></code></a> did not produce any result set or no call was issued yet.</p>
</dd></dl>

<dl class="method">
<dt id="cursor.scroll">
<code class="descname">scroll</code><span class="sig-paren">(</span><em>value</em><span class="optional">[</span>, <em>mode='relative'</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.scroll" title="Permalink to this definition"></a></dt>
<dd><p>Scroll the cursor in the result set to a new position according
to mode.</p>
<p>If <code class="xref py py-obj docutils literal"><span class="pre">mode</span></code> is <code class="docutils literal"><span class="pre">relative</span></code> (default), value is taken as offset to
the current position in the result set, if set to <code class="docutils literal"><span class="pre">absolute</span></code>,
value states an absolute target position.</p>
<p>If the scroll operation would leave the result set, a
<a class="reference internal" href="module.html#psycopg2.ProgrammingError" title="psycopg2.ProgrammingError"><code class="xref py py-obj docutils literal"><span class="pre">ProgrammingError</span></code></a> is raised and the cursor position is
not changed.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>According to the <a class="reference external" href="http://www.python.org/dev/peps/pep-0249/">DB API 2.0</a>, the exception raised for a cursor out
of bound should have been <code class="xref py py-obj docutils literal"><span class="pre">IndexError</span></code>.  The best option is
probably to catch both exceptions in your code:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
    <span class="n">cur</span><span class="o">.</span><span class="n">scroll</span><span class="p">(</span><span class="mi">1000</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">)</span>
<span class="k">except</span> <span class="p">(</span><span class="n">ProgrammingError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">),</span> <span class="n">exc</span><span class="p">:</span>
    <span class="n">deal_with_it</span><span class="p">(</span><span class="n">exc</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>The method can be used both for client-side cursors and
<a class="reference internal" href="usage.html#server-side-cursors"><span>server-side cursors</span></a>. Server-side cursors
can usually scroll backwards only if declared <a class="reference internal" href="#cursor.scrollable" title="cursor.scrollable"><code class="xref py py-obj docutils literal"><span class="pre">scrollable</span></code></a>.
Moving out-of-bound in a server-side cursor doesn&#8217;t result in an
exception, if the backend doesn&#8217;t raise any (Postgres doesn&#8217;t tell us
in a reliable way if we went out of bound).</p>
</dd></dl>

<dl class="attribute">
<dt id="cursor.arraysize">
<code class="descname">arraysize</code><a class="headerlink" href="#cursor.arraysize" title="Permalink to this definition"></a></dt>
<dd><p>This read/write attribute specifies the number of rows to fetch at a
time with <a class="reference internal" href="#cursor.fetchmany" title="cursor.fetchmany"><code class="xref py py-obj docutils literal"><span class="pre">fetchmany()</span></code></a>. It defaults to 1 meaning to fetch
a single row at a time.</p>
</dd></dl>

<dl class="attribute">
<dt id="cursor.itersize">
<code class="descname">itersize</code><a class="headerlink" href="#cursor.itersize" title="Permalink to this definition"></a></dt>
<dd><p>Read/write attribute specifying the number of rows to fetch from the
backend at each network roundtrip during <a class="reference internal" href="#cursor-iterable"><span>iteration</span></a> on a <a class="reference internal" href="usage.html#server-side-cursors"><span>named cursor</span></a>. The
default is 2000.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.itersize" title="cursor.itersize"><code class="xref py py-obj docutils literal"><span class="pre">itersize</span></code></a> attribute is a Psycopg extension to the DB API 2.0.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.rowcount">
<code class="descname">rowcount</code><a class="headerlink" href="#cursor.rowcount" title="Permalink to this definition"></a></dt>
<dd><p>This read-only attribute specifies the number of rows that the last
<a class="reference internal" href="#execute"><code class="xref py py-obj docutils literal"><span class="pre">execute*()</span></code></a> produced (for <abbr title="Data Query Language">DQL</abbr> statements
like <code class="sql docutils literal"><span class="pre">SELECT</span></code>) or affected (for
<abbr title="Data Manipulation Language">DML</abbr> statements like <code class="sql docutils literal"><span class="pre">UPDATE</span></code>
or <code class="sql docutils literal"><span class="pre">INSERT</span></code>).</p>
<p>The attribute is -1 in case no <code class="xref py py-obj docutils literal"><span class="pre">execute*()</span></code> has been performed on
the cursor or the row count of the last operation if it can&#8217;t be
determined by the interface.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference external" href="http://www.python.org/dev/peps/pep-0249/">DB API 2.0</a> interface reserves to redefine the latter case to
have the object return <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> instead of -1 in future versions
of the specification.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.rownumber">
<code class="descname">rownumber</code><a class="headerlink" href="#cursor.rownumber" title="Permalink to this definition"></a></dt>
<dd><p>This read-only attribute provides the current 0-based index of the
cursor in the result set or <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> if the index cannot be
determined.</p>
<p>The index can be seen as index of the cursor in a sequence (the result
set). The next fetch operation will fetch the row indexed by
<a class="reference internal" href="#cursor.rownumber" title="cursor.rownumber"><code class="xref py py-obj docutils literal"><span class="pre">rownumber</span></code></a> in that sequence.</p>
</dd></dl>

<span class="target" id="index-1"></span><dl class="attribute">
<dt id="cursor.lastrowid">
<code class="descname">lastrowid</code><a class="headerlink" href="#cursor.lastrowid" title="Permalink to this definition"></a></dt>
<dd><p>This read-only attribute provides the OID of the last row inserted
by the cursor. If the table wasn&#8217;t created with OID support or the
last operation is not a single record insert, the attribute is set to
<code class="xref py py-obj docutils literal"><span class="pre">None</span></code>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">PostgreSQL currently advices to not create OIDs on the tables and
the default for <a class="reference external" href="http://www.postgresql.org/docs/current/static/sql-createtable.html"><code class="sql docutils literal"><span class="pre">CREATE</span> <span class="pre">TABLE</span></code></a> is to not support them. The
<a class="reference external" href="http://www.postgresql.org/docs/current/static/sql-insert.html"><code class="sql docutils literal"><span class="pre">INSERT</span> <span class="pre">...</span> <span class="pre">RETURNING</span></code></a> syntax available from PostgreSQL 8.3 allows
more flexibility.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.query">
<code class="descname">query</code><a class="headerlink" href="#cursor.query" title="Permalink to this definition"></a></dt>
<dd><p>Read-only attribute containing the body of the last query sent to the
backend (including bound arguments) as bytes string. <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> if no
query has been executed yet:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;INSERT INTO test (num, data) VALUES (</span><span class="si">%s</span><span class="s">, </span><span class="si">%s</span><span class="s">)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="s">&#39;bar&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">query</span>
<span class="go">&quot;INSERT INTO test (num, data) VALUES (42, E&#39;bar&#39;)&quot;</span>
</pre></div>
</div>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.query" title="cursor.query"><code class="xref py py-obj docutils literal"><span class="pre">query</span></code></a> attribute is a Psycopg extension to the DB API 2.0.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.statusmessage">
<code class="descname">statusmessage</code><a class="headerlink" href="#cursor.statusmessage" title="Permalink to this definition"></a></dt>
<dd><p>Read-only attribute containing the message returned by the last
command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;INSERT INTO test (num, data) VALUES (</span><span class="si">%s</span><span class="s">, </span><span class="si">%s</span><span class="s">)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="s">&#39;bar&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">statusmessage</span>
<span class="go">&#39;INSERT 0 1&#39;</span>
</pre></div>
</div>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.statusmessage" title="cursor.statusmessage"><code class="xref py py-obj docutils literal"><span class="pre">statusmessage</span></code></a> attribute is a Psycopg extension to the
DB API 2.0.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.cast">
<code class="descname">cast</code><span class="sig-paren">(</span><em>oid</em>, <em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#cursor.cast" title="Permalink to this definition"></a></dt>
<dd><p>Convert a value from the PostgreSQL string representation to a Python
object.</p>
<p>Use the most specific of the typecasters registered by
<a class="reference internal" href="extensions.html#psycopg2.extensions.register_type" title="psycopg2.extensions.register_type"><code class="xref py py-obj docutils literal"><span class="pre">register_type()</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <a class="reference internal" href="#cursor.cast" title="cursor.cast"><code class="xref py py-obj docutils literal"><span class="pre">cast()</span></code></a> method is a Psycopg extension to the DB API 2.0.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="cursor.tzinfo_factory">
<code class="descname">tzinfo_factory</code><a class="headerlink" href="#cursor.tzinfo_factory" title="Permalink to this definition"></a></dt>
<dd><p>The time zone factory used to handle data types such as
<code class="sql docutils literal"><span class="pre">TIMESTAMP</span> <span class="pre">WITH</span> <span class="pre">TIME</span> <span class="pre">ZONE</span></code>.  It should be a <a class="reference external" href="/usr/share/doc/python3-doc/html/library/datetime.html#datetime.tzinfo" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">tzinfo</span></code></a>
object.  A few implementations are available in the <a class="reference internal" href="tz.html#module-psycopg2.tz" title="psycopg2.tz"><code class="xref py py-obj docutils literal"><span class="pre">psycopg2.tz</span></code></a>
module.</p>
</dd></dl>

<dl class="method">
<dt id="cursor.nextset">
<code class="descname">nextset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.nextset" title="Permalink to this definition"></a></dt>
<dd><p>This method is not supported (PostgreSQL does not have multiple data
sets) and will raise a <a class="reference internal" href="module.html#psycopg2.NotSupportedError" title="psycopg2.NotSupportedError"><code class="xref py py-obj docutils literal"><span class="pre">NotSupportedError</span></code></a> exception.</p>
</dd></dl>

<dl class="method">
<dt id="cursor.setoutputsize">
<code class="descname">setoutputsize</code><span class="sig-paren">(</span><em>size</em><span class="optional">[</span>, <em>column</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.setoutputsize" title="Permalink to this definition"></a></dt>
<dd><p>This method is exposed in compliance with the DB API 2.0. It currently
does nothing but it is safe to call it.</p>
</dd></dl>

<p class="rubric">COPY-related methods</p>
<div class="admonition-db-api-extension dbapi-extension admonition">
<p class="first admonition-title">DB API extension</p>
<p class="last">The <code class="sql docutils literal"><span class="pre">COPY</span></code> command is a PostgreSQL extension to the SQL standard.
As such, its support is a Psycopg extension to the DB API 2.0.</p>
</div>
<dl class="method">
<dt id="cursor.copy_from">
<code class="descname">copy_from</code><span class="sig-paren">(</span><em>file</em>, <em>table</em>, <em>sep='\t'</em>, <em>null='\\N'</em>, <em>size=8192</em>, <em>columns=None</em><span class="sig-paren">)</span><a class="headerlink" href="#cursor.copy_from" title="Permalink to this definition"></a></dt>
<dd><p>Read data <em>from</em> the file-like object <em>file</em> appending them to
the table named <em>table</em>.  See <a class="reference internal" href="usage.html#copy"><span>Using COPY TO and COPY FROM</span></a> for an overview.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>file</strong> &#8211; file-like object to read data from.  It must have both
<code class="xref py py-obj docutils literal"><span class="pre">read()</span></code> and <code class="xref py py-obj docutils literal"><span class="pre">readline()</span></code> methods.</li>
<li><strong>table</strong> &#8211; name of the table to copy data into.</li>
<li><strong>sep</strong> &#8211; columns separator expected in the file. Defaults to a tab.</li>
<li><strong>null</strong> &#8211; textual representation of <code class="sql docutils literal"><span class="pre">NULL</span></code> in the file.
The default is the two characters string <code class="docutils literal"><span class="pre">\N</span></code>.</li>
<li><strong>size</strong> &#8211; size of the buffer used to read from the file.</li>
<li><strong>columns</strong> &#8211; iterable with name of the columns to import.
The length and types should match the content of the file to read.
If not specified, it is assumed that the entire table matches the
file structure.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">&quot;42</span><span class="se">\t</span><span class="s">foo</span><span class="se">\n</span><span class="s">74</span><span class="se">\t</span><span class="s">bar</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">copy_from</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&#39;test&#39;</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;num&#39;</span><span class="p">,</span> <span class="s">&#39;data&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select * from test where id &gt; 5;&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span>
<span class="go">[(6, 42, &#39;foo&#39;), (7, 74, &#39;bar&#39;)]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>the name of the table is not quoted: if the table name
contains uppercase letters or special characters it must be quoted
with double quotes:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="n">cur</span><span class="o">.</span><span class="n">copy_from</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&#39;&quot;TABLE&quot;&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.0.6: </span>added the <em>columns</em> parameter.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>data read from files implementing the <a class="reference external" href="/usr/share/doc/python3-doc/html/library/io.html#io.TextIOBase" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">io.TextIOBase</span></code></a> interface
are encoded in the connection <a class="reference internal" href="connection.html#connection.encoding" title="connection.encoding"><code class="xref py py-obj docutils literal"><span class="pre">encoding</span></code></a> when sent to
the backend.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.copy_to">
<code class="descname">copy_to</code><span class="sig-paren">(</span><em>file</em>, <em>table</em>, <em>sep='\t'</em>, <em>null='\\N'</em>, <em>columns=None</em><span class="sig-paren">)</span><a class="headerlink" href="#cursor.copy_to" title="Permalink to this definition"></a></dt>
<dd><p>Write the content of the table named <em>table</em> <em>to</em> the file-like
object <em>file</em>.  See <a class="reference internal" href="usage.html#copy"><span>Using COPY TO and COPY FROM</span></a> for an overview.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>file</strong> &#8211; file-like object to write data into.  It must have a
<code class="xref py py-obj docutils literal"><span class="pre">write()</span></code> method.</li>
<li><strong>table</strong> &#8211; name of the table to copy data from.</li>
<li><strong>sep</strong> &#8211; columns separator expected in the file. Defaults to a tab.</li>
<li><strong>null</strong> &#8211; textual representation of <code class="sql docutils literal"><span class="pre">NULL</span></code> in the file.
The default is the two characters string <code class="docutils literal"><span class="pre">\N</span></code>.</li>
<li><strong>columns</strong> &#8211; iterable with name of the columns to export.
If not specified, export all the columns.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">copy_to</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="s">&#39;test&#39;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">&quot;|&quot;</span><span class="p">)</span>
<span class="go">1|100|abc&#39;def</span>
<span class="go">2|\N|dada</span>
<span class="gp">...</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>the name of the table is not quoted: if the table name
contains uppercase letters or special characters it must be quoted
with double quotes:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="n">cur</span><span class="o">.</span><span class="n">copy_to</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&#39;&quot;TABLE&quot;&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.0.6: </span>added the <em>columns</em> parameter.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>data sent to files implementing the <a class="reference external" href="/usr/share/doc/python3-doc/html/library/io.html#io.TextIOBase" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">io.TextIOBase</span></code></a> interface
are decoded in the connection <a class="reference internal" href="connection.html#connection.encoding" title="connection.encoding"><code class="xref py py-obj docutils literal"><span class="pre">encoding</span></code></a> when read
from the backend.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.copy_expert">
<code class="descname">copy_expert</code><span class="sig-paren">(</span><em>sql</em>, <em>file</em>, <em>size=8192</em><span class="sig-paren">)</span><a class="headerlink" href="#cursor.copy_expert" title="Permalink to this definition"></a></dt>
<dd><p>Submit a user-composed <code class="sql docutils literal"><span class="pre">COPY</span></code> statement. The method is useful to
handle all the parameters that PostgreSQL makes available (see
<a class="reference external" href="http://www.postgresql.org/docs/current/static/sql-copy.html"><code class="sql docutils literal"><span class="pre">COPY</span></code></a> command documentation).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>sql</strong> &#8211; the <code class="sql docutils literal"><span class="pre">COPY</span></code> statement to execute.</li>
<li><strong>file</strong> &#8211; a file-like object to read or write (according to <em>sql</em>).</li>
<li><strong>size</strong> &#8211; size of the read buffer to be used in <code class="sql docutils literal"><span class="pre">COPY</span> <span class="pre">FROM</span></code>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>The <em>sql</em> statement should be in the form <code class="samp docutils literal"><span class="pre">COPY</span> <em><span class="pre">table</span></em> <span class="pre">TO</span>
<span class="pre">STDOUT</span></code> to export <code class="samp docutils literal"><em><span class="pre">table</span></em></code> to the <em>file</em> object passed as
argument or <code class="samp docutils literal"><span class="pre">COPY</span> <em><span class="pre">table</span></em> <span class="pre">FROM</span> <span class="pre">STDIN</span></code> to import the content of
the <em>file</em> object into <code class="samp docutils literal"><em><span class="pre">table</span></em></code>.</p>
<p><em>file</em> must be a readable file-like object (as required by
<a class="reference internal" href="#cursor.copy_from" title="cursor.copy_from"><code class="xref py py-obj docutils literal"><span class="pre">copy_from()</span></code></a>) for <em>sql</em> statement <code class="sql docutils literal"><span class="pre">COPY</span> <span class="pre">...</span> <span class="pre">FROM</span> <span class="pre">STDIN</span></code>
or a writable one (as required by <a class="reference internal" href="#cursor.copy_to" title="cursor.copy_to"><code class="xref py py-obj docutils literal"><span class="pre">copy_to()</span></code></a>) for <code class="sql docutils literal"><span class="pre">COPY</span>
<span class="pre">...</span> <span class="pre">TO</span> <span class="pre">STDOUT</span></code>.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">copy_expert</span><span class="p">(</span><span class="s">&quot;COPY test TO STDOUT WITH CSV HEADER&quot;</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
<span class="go">id,num,data</span>
<span class="go">1,100,abc&#39;def</span>
<span class="go">2,,dada</span>
<span class="gp">...</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.6.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>files implementing the <a class="reference external" href="/usr/share/doc/python3-doc/html/library/io.html#io.TextIOBase" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">io.TextIOBase</span></code></a> interface are dealt with
using Unicode data instead of bytes.</p>
</div>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="connection.html"
                        title="previous chapter">The <code class="docutils literal"><span class="pre">connection</span></code> class</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="advanced.html"
                        title="next chapter">More advanced topics</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/cursor.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>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <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> |</li>
        <li class="right" >
          <a href="advanced.html" title="More advanced topics"
             >next</a> |</li>
        <li class="right" >
          <a href="connection.html" title="The connection class"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Psycopg 2.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2001-2016, Federico Di Gregorio, Daniele Varrazzo.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.4.
    </div>
  </body>
</html>