python - 使用python查询SQL

标签 python sql oracle

我尝试使用 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 ???????

这是数据库中的值。

OOP แม่เจ้า

我希望预期值为OOP แม่เจ้า(泰语)。

你能帮我一下吗?

最佳答案

例如,在启动 Python 之前设置 Oracle NLS_LANG 环境变量

export NLS_LANG=.AL32UTF8

(是的,“=”后面有一个句号)。

有关全局化的 Oracle 文档是 Database Globalization Support Guide

[更新] @anthony-tuininga pointed out您可以简单地执行以下操作:

导入cx_Oracle conn = cx_Oracle.connect(connectString, 编码 = "UTF-8", nencoding = "UTF-8")

在此示例中不需要 NLS_LANG。如果您不使用 NCHAR 数据,则可以省略 nencoding 选项。

关于python - 使用python查询SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320867/

相关文章:

python - 在使用特定 python 的 virtualenv 中时,pip 找不到tensorflow

python - 值错误 : could not convert string to float: '−4.0'

python - "filedescriptor out of range in select()"将 python 的子进程与 rsync 一起使用时

SQL:Oracle - 查询中的参数

java - 如何在android/java中使用oracle utl_raw?

python - 查找满足条件的 numpy 数组元素

sql - 用不存在的行完成表格

sql - MySQL如何在单个查询中执行if exist increment

mysql - 城镇报告(订单数)

sql - 计算每种类型每天插入的行数