|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +# Copyright 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 | +# UniversalRowids.py |
| 12 | +# This script demonstrates the use of universal rowids. Universal rowids are |
| 13 | +# used to identify rows in index organized tables. |
| 14 | +# |
| 15 | +# This script requires cx_Oracle 6.0 and higher. |
| 16 | +#------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +import cx_Oracle |
| 21 | +import datetime |
| 22 | + |
| 23 | +DATA = [ |
| 24 | + (1, "String #1", datetime.datetime(2017, 4, 4)), |
| 25 | + (2, "String #2", datetime.datetime(2017, 4, 5)), |
| 26 | + (3, "A" * 250, datetime.datetime(2017, 4, 6)) |
| 27 | +] |
| 28 | + |
| 29 | +# truncate table so sample can be rerun |
| 30 | +connection = cx_Oracle.Connection("cx_Oracle/dev@localhost/orcl") |
| 31 | +cursor = connection.cursor() |
| 32 | +print("Truncating table...") |
| 33 | +cursor.execute("truncate table TestUniversalRowids") |
| 34 | + |
| 35 | +# populate table with a few rows |
| 36 | +print("Populating table...") |
| 37 | +for row in DATA: |
| 38 | + print("Inserting", row) |
| 39 | + cursor.execute("insert into TestUniversalRowids values (:1, :2, :3)", row) |
| 40 | +connection.commit() |
| 41 | + |
| 42 | +# fetch the rowids from the table |
| 43 | +rowids = [r for r, in cursor.execute("select rowid from TestUniversalRowids")] |
| 44 | + |
| 45 | +# fetch each of the rows given the rowid |
| 46 | +for rowid in rowids: |
| 47 | + print("-" * 79) |
| 48 | + print("Rowid:", rowid) |
| 49 | + cursor.execute(""" |
| 50 | + select IntCol, StringCol, DateCol |
| 51 | + from TestUniversalRowids |
| 52 | + where rowid = :rid""", |
| 53 | + rid = rowid) |
| 54 | + intCol, stringCol, dateCol = cursor.fetchone() |
| 55 | + print("IntCol:", intCol) |
| 56 | + print("StringCol:", stringCol) |
| 57 | + print("DateCol:", dateCol) |
| 58 | + |
0 commit comments