2017-06-02 41 views
1

我嘗試使用python查詢SQL,但它返回問號。使用python查詢SQL

這是我的代碼。

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 

這是實際結果。

OOP ??????? 

這是DB中的值。

OOP แม่เจ้า 

我想要的預期值是OOP แม่เจ้า(泰文)。

你能幫我嗎?

+0

如果你修改這個'''select book_name from book where id ='100'''''''選擇book_name from book where id = 100「'? – arnold

+0

你需要修復你的編碼。 – hd1

回答

2

集開始的Python,例如

export NLS_LANG=.AL32UTF8 

(是的,有在 '=' 後完全停止)之前在Oracle NLS_LANG環境變量。

全球化Oracle文檔是Database Globalization Support Guide

[更新] @安東尼 - tuininga pointed out,你可以簡單地做:

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

有在這個例子中不需要NLS_LANG。如果您不使用NCHAR數據,則可以省略nencoding選項。