简体   繁体   English

pyodbc 数据库连接和数字格式

[英]pyodbc database connection and number format

I'm connecting a Python algorithm to an Oracle database using pypyodbc library.我正在使用 pypyodbc 库将 Python 算法连接到 Oracle 数据库。

I'm using the following:我正在使用以下内容:

def connectToServer(DSN,Server,Password):
    connString = "DSN="+DSN+";Uid="+Server+";Pwd="+Password
    connObject = odbc.connect(connString)

    return connObject

connObject = connectToServer('xxx','xxx','xxx')

Connection works well, i can pass commands to the server (create, drop tables, execute procedure etc...)连接运行良好,我可以将命令传递给服务器(创建、删除表、执行过程等...)

However i cannot extract data via the following:但是我无法通过以下方式提取数据:

# print top lines of a given table
def selectTopFromTable(connObject,tableName,numberRow=100):
    cur = connObject.cursor()
    command = '''SELECT * FROM ''' + tableName +''' WHERE ROWNUM <= ''' + str(numberRow) + '''; '''  
    cur.execute(command)

    for row in cur.fetchall():
        print(row)

selectTopFromTable(connObject,'xxx')

Getting the following error:得到以下错误:

ValueError: could not convert string to float: b'5,3' ValueError:无法将字符串转换为浮点数:b'5,3'

I think the issue lies in the format number FR versus EN (i'm working on a FR based server) - the server provides values in FR format number (ie 5,3 instead of 5.3) and Python is confused when trying to convert a supposedly NUMBER SQL type into a double.我认为问题在于格式编号 FR 与 EN(我正在基于 FR 的服务器上工作)-服务器提供 FR 格式编号的值(即 5,3 而不是 5.3),并且 Python 在尝试转换时感到困惑推测 NUMBER SQL 类型为双精度。

Is it possible to configure the pypyodbc connection to retrieve value in EN number format?是否可以配置 pypyodbc 连接以检索 EN 数字格式的值? (the same way a client such as SQL Developer or Toad is set up to do so)? (与 SQL Developer 或 Toad 等客户端的设置方式相同)?

Here's how you can convert your locale to be France in Python:以下是在 Python 中将语言环境转换为 France 的方法:

import locale

locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
locale.atof('1000,53')

This converts the string to a floating point number, with the settings for the locale of France.这会将字符串转换为浮点数,并使用法国语言环境的设置。 I'm not sure exactly how to tie that in with Oracle (I haven't used Oracle since the 1990s), but hopefully this puts you on the right path.我不确定如何将其与 Oracle 联系起来(自 1990 年代以来我就没有使用过 Oracle),但希望这能让您走上正确的道路。 You could cast it as a string and then convert using this method, but there may be a better way at the DB level.您可以将其转换为字符串,然后使用此方法进行转换,但在 DB 级别可能有更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM