2

I have an issue when displaying a value in python retrieved from oracle table into CLOB field:

Oracle query:

SELECT EXTRACTVALUE(xmltype(t.xml), '/DCResponse/ResponseInfo/ApplicationId') 
  FROM table t 
 WHERE id = 2

Value displayed in Oracle Client

5701200

Python code

import cx_Oracle 
conn = cx_Oracle.Connection("user/pwd@localhost:1521/orcl")
cursor = conn.cursor()
cursor.execute("""SELECT EXTRACTVALUE(xmltype(t.xml),'/DCResponse/ResponseInfo/ApplicationId') FROM table t where id = 2""")
for row in cursor:
print(row)

Python Console: Nothing is displayed!!! I want to show:5701200

Please Help. Best Regards Giancarlo

2 Answers 2

1

There are only a few issues with your code :

  • Replace cx_Oracle.Connection with cx_Oracle.connect
  • Be careful about the indentation related to the print(row)
  • Triple double-quotes, within the SELECT statement, are redundant, replace them with Single double-quotes
  • Prefer Using print(row[0]) in order to return the desired number rather than a tuple printed.

    import cx_Oracle 
    conn = cx_Oracle.connect('user/pwd@localhost:1521/orcl')
    cursor = conn.cursor()
    
    query  = "SELECT EXTRACTVALUE(xmltype(t.xml),'/DCResponse/ResponseInfo/ApplicationId')"
    query += "  FROM tab t "
    query += " WHERE t.ID = 2 "
    
    cursor.execute( query )
    for row in cursor:
        print(row[0])
    

Assigning a query to a variable not required, as stated in my case, but preferable to use in order to display the long SELECT statement decently.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to iterate over result, use this one:

for row in cursor.execute("sql_query")
    print(row)

or you can fetch each row like this:

cursor = conn.cursor()
cursor.execute("sql_query")
while True:
    row = cursor.fetchone()
    print(row)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.