So to complete Kevins example for anyone else who wants to try this out:
Visit this page to install all the DB2 addons if you're just getting started with Python 2 or 3
https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/IBM%20i%20Technology%20Updates/page/Installing%20shipped%20add-ons
Install ptable as Kevin mentioned
To install, just run from a PASE shell: pip3 install ptable
Enter the following python script in an IFS file named: "/tmp/dbhtml.py" or whatever you want:
#!/usr/bin/env python3
import ibm_db_dbi as db2
from prettytable import from_db_cursor
query = "select * from qiws.qcustcdt"
outputfile = "/tmp/htmlfile.html"
#Connection string with database and specific credentials
#database = "DATABASE=*LOCAL;UID=USER1;PWD=PASS1;"
#Connection string with just database - Appears to run as logged in user ID.
database = "DATABASE=*LOCAL"
#Connect to the database and get a connection
conn = db2.connect(database)
# Get a cursor and run the query
cur = conn.cursor()
cur.execute(query)
#Output the HTML
table = from_db_cursor(cur)
with open(outputfile, "w") as file:
file.write("""<!DOCTYPE HTML>
<html lang="en-US">
<head>
<title>Table to HTML</title>
<head>
<body>
{}
</body>
</html>""".format(table.get_html_string()))
Have fun.
Regards,
Richard Schoen
Director of Document Management
e. richard.schoen@xxxxxxxxxxxxxxx
p. 952.486.6802
w. helpsystems.com
-----Original Message-----
From: Richard Schoen
Sent: Friday, January 12, 2018 10:00 AM
To: midrange-l@xxxxxxxxxxxx
Subject: RE: Convert a database file to Html format - Python perhaps ?
Hey Kevin,
I'm getting this error with your example:
ImportError: No module named ibm_db_dbi
Is there a path setup thing to find the db2 modules ?
Looks like they are installed.
Regards,
Richard Schoen
Director of Document Management
e. richard.schoen@xxxxxxxxxxxxxxx
p. 952.486.6802
w. helpsystems.com
------------------------------
message: 5
date: Wed, 10 Jan 2018 17:49:21 +0000
from: "Kevin Adler" <kadler@xxxxxxxxxx>
subject: RE: Convert a database file to Html format - Python perhaps ?
Super easy in Python. If you're using the OPS Python, you can use
prettytable: [1]
https://pypi.python.org/pypi/PrettyTable
To install, just run from a PASE shell: pip3 install ptable
#!/usr/bin/env python3
import ibm_db_dbi as db2
from prettytable import from_db_cursor
query = "select * from qiws.qcustcdt"
conn = db2.connect()
cur = conn.cursor()
cur.execute(query)
table = from_db_cursor(cur)
with open("file.html", "w") as file:
file.write("""<!DOCTYPE HTML>
<html lang="en-US">
<head>
<title>Table to HTML</title>
<head>
<body>
{}
</body>
</html>""".format(table.get_html_string()))
References
Visible links
1.
https://pypi.python.org/pypi/PrettyTable
As an Amazon Associate we earn from qualifying purchases.