• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

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