• The new Python extension is now available. It allows customers to deploy and manage WSGI-based Python applications on their websites directly from Plesk.
  • The Plesk Mobile apps for iOS and Android and the Plesk Mobile Center extension have been deprecated, and are no longer available for new installations. In Plesk Obsidian 18.0.84, the Plesk Mobile Center extension will be removed from servers during the upgrade, and the Plesk Mobile apps will lose their server management features.
  • The Social Login extension has been deprecated and will be removed in Plesk Obsidian 18.0.84. After that, it will no longer be possible to log in to Plesk with a Google, Facebook, GitHub account, or with a passkey. Plesk usernames and passwords, as well as logging in with a WebPros Account, are not affected.

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