• Our team is looking to connect with folks who use email services provided by Plesk, or a premium service. If you'd like to be part of the discovery process and share your experiences, we invite you to complete this short screening survey. If your responses match the persona we are looking for, you'll receive a link to schedule a call at your convenience. We look forward to hearing from you!
  • The BIND DNS server has already been deprecated and removed from Plesk for Windows.
    If a Plesk for Windows server is still using BIND, the upgrade to Plesk Obsidian 18.0.70 will be unavailable until the administrator switches the DNS server to Microsoft DNS. We strongly recommend transitioning to Microsoft DNS within the next 6 weeks, before the Plesk 18.0.70 release.
  • The Horde component is removed from Plesk Installer. We recommend switching to another webmail software supported in Plesk.

Question Retrieving Data from DB2 Using pyodbc

elenaflorence

New Pleskian
I am trying to retrieve data from an DB2 using pyodbc and print it in JSON using Python. I have 2 different func.
This one's works :
import flask
from flask import flash, request, Flask, jsonify
import pyodbc

@app.route('/api/acrdkl/filter', methods=['GET'])
def api_filter():

dkkdcb = request.args.get('dkkdcb')
dkblrg = request.args.get('dkblrg')
dkthrg = request.args.get('dkthrg')

query = """select trim(dkkdcb), trim(dkkdps), trim(dkcob), trim(dkureg), trim(dkbktg), trim(dkblrg), trim(dkthrg)
from simdta.ACRDKL WHERE dkkdcb = param_dkkdcb and dkblrg = param_dkblrg and dkthrg = param_dkthrg"""

print("Dkkdcb ", dkkdcb)
print("Dkblrg ", dkblrg)
print("Dkthrg ", dkthrg)

query = query.replace("param_dkkdcb", dkkdcb)
query = query.replace("param_dkblrg", dkblrg)
query = query.replace("param_dkthrg", dkthrg)
print("Query 1 ", query)

conn = pyodbc.connect("DSN=AS400;UID=.....;PWD=....")
cur = conn.cursor()
cur.execute(query)
rows = cur.fetchall()
results = []
for dt in rows:
print("DT ", dt)
print("dkkdcb: ", dt[0])
print("dkkdps: ", dt[1])
print("dkcob: ", dt[2])
print("dkureg: ", dt[3])
print("dkbktg: ", dt[4])
print("dkblrg: ", dt[5])
print("dkthrg: ", dt[6])

print("\n")
results.append([x for x in dt])
return jsonify(results)

But when I tried this one's using try-except:
@app.route('/api/acrdkl/filter2', methods=['GET'])
def filter2():
try:
cnxn = pyodbc.connect("DSN=AS400;UID=....;PWD=....")
cursor = cnxn.cursor()
sql= """select
trim(dkkdcb), trim(dkkdps), trim(dkcob), trim(dkureg), trim(dkbktg), trim(dkblrg), trim(dkthrg)
from simdta.ACRDKL where dkkdcb=? and dkblrg=? and dkthrg=?"""
data=(int(ardkkdcb), month, year)
cursor.execute(sql, 1402, 10, 2020)
rows = cursor.fetchall()
result = []
for dt in rows:
print("dkkdcb: ", dt[0])
print("dkkdps: ", dt[1])
print("dkcob: ", dt[2])
print("dkureg: ", dt[3])
print("dkbktg: ", dt[4])
print("dkblrg: ", dt[5])
print("dkthrg: ", dt[6])

print("\n")
result.append([x for x in dt])
return json.dumps(result)
except Exception as e:
return(str(e))

It gives me this error :
('22023', '[22023] [Microsoft][ODBC DB2 Driver]Data exception - SQLSTATE 22023, SQLCODE -302. SQLSTATE: 22023, SQLCODE: -302 (-302) (SQLExecDirectW)')

Am I missing something in the second func? If there is a better way to solve would be appreciated. Thanks in advance.
 

Similar threads

Back
Top