Skip to content

Instantly share code, notes, and snippets.

@mbiette
Last active October 6, 2019 21:54
Show Gist options
  • Save mbiette/6cfd5b2dc2624c094575 to your computer and use it in GitHub Desktop.
Save mbiette/6cfd5b2dc2624c094575 to your computer and use it in GitHub Desktop.
Connect to an AS/400 (iSeries) using ODBC.
import pyodbc
class CommitMode:
NONE = 0 #Commit immediate (*NONE) --> QSQCLIPKGN
CS = 1 #Read committed (*CS) --> QSQCLIPKGS
CHG = 2 #Read uncommitted (*CHG) --> QSQCLIPKGC
ALL = 3 #Repeatable read (*ALL) --> QSQCLIPKGA
RR = 4 #Serializable (*RR) --> QSQCLIPKGL
class ConnectionType:
ReadWrite = 0 #Read/Write (all SQL statements allowed)
ReadCall = 1 #Read/Call (SELECT and CALL statements allowed)
Readonly = 2 #Read-only (SELECT statements only)
def connstr(system, commitmode=None, connectiontype=None):
_connstr = 'DRIVER=iSeries Access ODBC Driver;'+\
'SYSTEM='+system+';'+\
'SIGNON=4;CCSID=1208;TRANSLATE=1;'
if commitmode is not None:
_connstr = _connstr + 'CommitMode=' + str(commitmode) + ';'
if connectiontype is not None:
_connstr = _connstr +'ConnectionType=' + str(connectiontype) + ';'
return _connstr
def main():
with pyodbc.connect(connstr("SYSTEM",CommitMode.CHG,ConnectionType.Readonly)) as db:
cursor = db.cursor()
cursor.execute(
"""
SELECT * FROM IASP.LIB.FILE
""")
for row in cursor:
print(' '.join(map(str,row)))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment