python - 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
。
数据报告哪个f1
是Decimal('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)
解决方案
推荐阅读
- android - 如何初始化 LinearLayout 变量?
- swift - 如何从 NSDictionary 检索引号之间的 Int
- c - 从类型 'int (*)[1000] 分配给类型 'int[100][1000]' 时的类型不兼容
- node.js - 通过原始回调模拟方法和调用
- laravel - Laravel 在用户选择的指定日期和时间发送电子邮件提醒
- flutter - 应该在那个“流:”部分中传递什么?
- bash - 使用 sed 从字符串中删除 ANSI 代码
- xcode - 如何修复在模拟器/设备上完美运行但存档失败的 Xcode 构建?
- javascript - React w Gatsby:为画廊实现了顺序淡入动画,但在部署后无法正常工作
- javascript - 在 OnSubmitEditing 中使用时超出最大更新深度