-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbconn.py
66 lines (57 loc) · 2.26 KB
/
dbconn.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
54
55
56
57
58
59
60
61
62
63
64
65
66
'''
----------------------------------------------------------------------------------------------------
Copyright (c) 2019 StateStreetOpenSource
All rights reserved.
Program Name: dbconn.py
Program Description: Establish a database connection to DB2 on IBM i from a remote Linux box
or locally from the IBM i.
Redistribution of this code, with or without modification, is permitted providing the following
conditions are met.
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimers.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE AUTHOR'S EMPLOYER BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION),
HOWEVER CAUSED (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Usage...
c1, conn = db2conn()
sqlstr = "select some_field from some.file"
c1.execute(sqlstr)
for row in c1.fetchall():
myField = row[0]
conn.close()
----------------------------------------------------------------------------------------------------
'''
try:
import ibm_db
import ibm_db_dbi
conType = 'LOCAL'
except:
try:
import pyodbc
conType = 'REMOTE'
except:
conType = 'UNDEFINED'
def db2conn():
try:
if conType == 'LOCAL':
conn = ibm_db_dbi.connect('DATABASE=*LOCAL')
elif conType == 'REMOTE':
conn = pyodbc.connect(
DRIVER='IBM i Access ODBC Driver',
SYSTEM='your IBMi IP',
UID='IBM I USERNAME',
PWD='IBM I PASSWORD')
else:
return None, None
c1 = conn.cursor()
return c1, conn
except:
if conType == 'LOCAL':
print("Connection Error:", ibm_db.conn_errormsg())
else:
print("Connection Error:", conn.conn_errormsg())
return None, None