|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +# Copyright 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
| 3 | +# |
| 4 | +# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved. |
| 5 | +# |
| 6 | +# Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta, |
| 7 | +# Canada. All rights reserved. |
| 8 | +#------------------------------------------------------------------------------ |
| 9 | + |
| 10 | +#------------------------------------------------------------------------------ |
| 11 | +# ScrollableCursors.py |
| 12 | +# This script demonstrates how to use scrollable cursors. These allow moving |
| 13 | +# forward and backward in the result set but incur additional overhead on the |
| 14 | +# server to retain this information. |
| 15 | +#------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +from __future__ import print_function |
| 18 | + |
| 19 | +import cx_Oracle |
| 20 | + |
| 21 | +con = cx_Oracle.connect("cx_Oracle/dev@localhost/orcl") |
| 22 | + |
| 23 | +# show all of the rows available in the table |
| 24 | +cur = con.cursor() |
| 25 | +cur.execute("select * from TestStrings order by IntCol") |
| 26 | +print("ALL ROWS") |
| 27 | +for row in cur: |
| 28 | + print(row) |
| 29 | +print() |
| 30 | + |
| 31 | +# create a scrollable cursor |
| 32 | +cur = con.cursor(scrollable = True) |
| 33 | + |
| 34 | +# set array size smaller than the default (100) to force scrolling by the |
| 35 | +# database; otherwise, scrolling occurs directly within the buffers |
| 36 | +cur.arraysize = 3 |
| 37 | +cur.execute("select * from TestStrings order by IntCol") |
| 38 | + |
| 39 | +# scroll to last row in the result set; the first parameter is not needed and |
| 40 | +# is ignored) |
| 41 | +cur.scroll(mode = "last") |
| 42 | +print("LAST ROW") |
| 43 | +print(cur.fetchone()) |
| 44 | +print() |
| 45 | + |
| 46 | +# scroll to the first row in the result set; the first parameter not needed and |
| 47 | +# is ignored |
| 48 | +cur.scroll(mode = "first") |
| 49 | +print("FIRST ROW") |
| 50 | +print(cur.fetchone()) |
| 51 | +print() |
| 52 | + |
| 53 | +# scroll to an absolute row number |
| 54 | +cur.scroll(5, mode = "absolute") |
| 55 | +print("ROW 5") |
| 56 | +print(cur.fetchone()) |
| 57 | +print() |
| 58 | + |
| 59 | +# scroll forward six rows (the mode parameter defaults to relative) |
| 60 | +cur.scroll(3) |
| 61 | +print("SKIP 3 ROWS") |
| 62 | +print(cur.fetchone()) |
| 63 | +print() |
| 64 | + |
| 65 | +# scroll backward four rows (the mode parameter defaults to relative) |
| 66 | +cur.scroll(-4) |
| 67 | +print("SKIP BACK 4 ROWS") |
| 68 | +print(cur.fetchone()) |
| 69 | +print() |
| 70 | + |
0 commit comments