1

I try to query SQL by using python but it returns question mark.

This is my code.

import cx_Oracle
import importlib

class CustomDatabase(object):

    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

    def __init__(self):
        """
        Initializes _dbconnection to None.
        """
        self._dbconnection = None
        self.db_api_module_name = None

    def GetDataFromDatabaseByRow(self, db_connect_string, selectStatement, row):
        db_api_2 = importlib.import_module("cx_Oracle")
        db_connect_string = 'db_api_2.connect(%s)' % db_connect_string
        self.db_api_module_name = "cx_Oracle"
        self._dbconnection = eval(db_connect_string)
        row = int(row)
        cur = None
        try:
            cur = self._dbconnection.cursor()
            cur.execute(selectStatement)
            res = cur.fetchone()
            i = 0
            while (i < row):
                if cur.rowcount == row:
                    data = res
                res = cur.fetchone()
                i = i + 1
                if i == row:
                    for x in data:  
                        result=x
            #return result.decode('iso-8859-11')
            return result
        finally :
            if cur :
                self._dbconnection.rollback()

Obj=CustomDatabase()
A=Obj.GetDataFromDatabaseByRow("'BELL', 'BELL#', '192.168.1.10:110/BELL'","select book_name from book where id='100'",1)
print A

This is actual result.

OOP ???????

This is value in DB.

OOP แม่เจ้า

I want expected value to be OOP แม่เจ้า (Thai language).

Could you please help me?

2
  • what if you modify this one "select book_name from book where id='100'" into "select book_name from book where id=100"? Commented Jun 2, 2017 at 4:47
  • You need to fix your encoding. Commented Jun 2, 2017 at 5:34

1 Answer 1

2

Set the Oracle NLS_LANG environment variable before starting Python, for example

export NLS_LANG=.AL32UTF8

(yes, there is a full stop after the '=').

Oracle documentation on globalization is Database Globalization Support Guide

[Updated] @anthony-tuininga pointed out that you can simply do:

import cx_Oracle conn = cx_Oracle.connect(connectString, encoding = "UTF-8", nencoding = "UTF-8")

There is no need for NLS_LANG in this example. The nencoding option can be omitted if you are not using NCHAR data.

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

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.