-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOracle.py
More file actions
73 lines (60 loc) · 1.78 KB
/
Copy pathOracle.py
File metadata and controls
73 lines (60 loc) · 1.78 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import oracledb
# Connection constants
username = 'ds2'
password = 'ds2'
hostname = '127.0.0.1'
port_num = 1521
instance = 'XE'
# Properties of the psycopg2 library.
db_lib_name = 'psycopg2'
if db_lib_name in {'psycopg2', 'pymysql'}:
db_lib_version = oracledb.__version__
else:
db_lib_version = oracledb.version
print("DB Library Version:", db_lib_version)
print("oracledb parameter style ('named', 'qmark', or 'pyformat'):", oracledb.paramstyle)
# paramstyle = 'named': oracledb. Option for sqlite3 & psycopg2.
# paramstyle = 'qmark': sqlite3 and pyodbc.
# paramstyle = 'pyformat': pymysql and psycopg2.
# Make connection.
connect_string = f"{username}/{password}@{hostname}:{port_num}/{instance}"
print(connect_string)
connection = oracledb.connect(connect_string)
print("Created connection.")
# Properties and methods of the connection object.
# connection.interrupt()
# Create cursor.
cursor = connection.cursor()
print("Created cursor.")
# SELECT
SQL = "SELECT * FROM Categories"
cursor.execute(SQL)
print(f"Executed SQL: {SQL}")
columns = [item[0] for item in cursor.description]
columns = ', '.join(columns)
print(f"Columns in result set: '{columns}'")
# The result set.
rows = cursor.fetchall()
print("Rows in result set:")
for row in rows:
print(row)
# SQL with Bind Variables.
SQL = "SELECT * FROM Categories WHERE category = :var1 OR categoryname = :var2"
# Either format is acceptable.
cursor.execute(SQL, {"var1": 1, "var2": "Drama"})
# cursor.execute(SQL, [1, "Drama"])
# The result set.
rows = cursor.fetchall()
print("Rows in result set:")
for row in rows:
print(row)
"""
# Properties and methods of the cursor object.
# cursor.rollback()
# cursor.connection
# print(cursor.rowcount), often -1 for psycopg2
"""
# Finish up.
connection.commit()
cursor.close()
connection.close()