Skip to content

Commit 113c31e

Browse files
Add samples for implicit results and scrollable cursors.
1 parent 6da6c42 commit 113c31e

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

samples/ImplicitResults.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
# ImplicitResults.py
12+
# This script demonstrates the use of the 12.1 feature that allows PL/SQL
13+
# procedures to return result sets implicitly, without having to explicitly
14+
# define them.
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+
cur = con.cursor()
23+
24+
# use PL/SQL block to return two cursors
25+
cur.execute("""
26+
declare
27+
c1 sys_refcursor;
28+
c2 sys_refcursor;
29+
begin
30+
31+
open c1 for
32+
select * from TestNumbers;
33+
34+
dbms_sql.return_result(c1);
35+
36+
open c2 for
37+
select * from TestStrings;
38+
39+
dbms_sql.return_result(c2);
40+
41+
end;""")
42+
43+
# display results
44+
for ix, resultSet in enumerate(cur.getimplicitresults()):
45+
print("Result Set #" + str(ix + 1))
46+
for row in resultSet:
47+
print(row)
48+
print()
49+

samples/ScrollableCursors.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)