• The APS Catalog has been deprecated and removed from all Plesk Obsidian versions.
    Applications already installed from the APS Catalog will continue working. However, Plesk will no longer provide support for APS applications.
  • Please be aware: with the Plesk Obsidian 18.0.78 release, the support for the ngx_pagespeed.so module will be deprecated and removed from the sw-nginx package.

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.
 
Back
Top