首页 > 解决方案 > Python 和 pyodbc 与 SQL Server。当精度增加时,如何从 SQL Server 获取十进制数据?

问题描述

我有一个带有两列f1&的 SQL Server 数据库表f2。这些在 SQL Server 中定义为

f1 DECIMAL (10, 4)
f2 DECIMAL (13, 4)

当我选择使用 python 和 pyodbc 并加载数据时:

(select f1 , f2 from table where condition) 
data = cursor.fetchone()

我没有得到正确的数据f2

数据报告哪个f1Decimal('16.7300')正确的。

f2数据库中的值为 40.000,但查询正在返回Decimal('0.0000')

(Decimal('16.7300'), Decimal('0.0000'))

我已经尝试对查询进行强制转换

cast(f2 as decimal(13,4))

但查询仍然返回 0。

谁能建议如何从数据库中返回正确的值?

作为对@Panagiotis Kanavos 的回复,这里是确切的代码。

import pyodbc
conn = pyodbc.connect(driver ='{SQL Server}', server = 'ServerName', database = 'db')
cursor = conn.cursor()
sql = '''select f1 , f2 from dbo.Table where Company = 'Acme' '''
cursor.execute(sql)
data = cursor.fetchone()
print (data)

标签: pythonsql-serverpyodbc

解决方案


推荐阅读