forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.py
53 lines (42 loc) · 1.39 KB
/
Example.py
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
#------------------------------------------------------------------------------
# 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 Python and the cx_Oracle interface
# - 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)