From 481b1457871582165b4f330639012d9249825f33 Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Tue, 7 Aug 2018 11:38:38 +1000 Subject: [PATCH] Add a generic Cloud service example for Python cx_Oracle --- exadata-express/Example.py | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 exadata-express/Example.py diff --git a/exadata-express/Example.py b/exadata-express/Example.py new file mode 100644 index 00000000..e42fe10c --- /dev/null +++ b/exadata-express/Example.py @@ -0,0 +1,52 @@ +#------------------------------------------------------------------------------ +# Example.py +# +# Demonstrate how to perform a database insert and query with Python +# in Oracle Database Cloud services such as Exadata Express, +# Autonomous Transaction Processing, Autonomous Data Warehouse, and +# others. +# +# Before running this script: +# - Install Oracle Instant Client +# - Download and install the cloud service wallet +# - Modify the connect() call below to use the credentials for your database. +# See your cloud service's documentation for details. +# +#------------------------------------------------------------------------------ + +from __future__ import print_function + +import cx_Oracle + +con = cx_Oracle.connect('username', 'password', 'connect_string') +cur = con.cursor() + +# Create a table + +cur.execute(""" +begin + execute immediate 'drop table mycloudtab'; + exception + when others then + if sqlcode not in (-00942) then + raise; + end if; +end; +"""); + +cur.execute('create table mycloudtab (id number, data varchar2(20))') + +# Insert some data + +rows = [ (1, "First" ), (2, "Second" ), + (3, "Third" ), (4, "Fourth" ), + (5, "Fifth" ), (6, "Sixth" ), + (7, "Seventh" ) ] + +cur.executemany("insert into mycloudtab(id, data) values (:1, :2)", rows) + +# Query the data + +cur.execute('select * from mycloudtab') +res = cur.fetchall() +print(res)