我得到这个问题,我使用撷取结果(使用fetchall在这个例子中)的任何pyodbc API中提取方法,十进制值取整到两个地方。在MSSQL数据库中,更特别是在SQL管理工作室中,当我执行存储过程时,余额,借方和贷方的值正确显示为4个地方。pyodbc SQLSERVER光标小数四舍五入
我试图格式化结果集我使用fetchall后它会显示四个小数位,但它仍然四舍五入它虽然。例如14.5272 => 14.5300。
在模板还我尝试使用floatformat过滤无济于事它仍然保持四舍五入:
<td >{{ Accounts.debits|floatformat:4 }}</td>
models.py
from django.core.cache import cache
from django.conf import settings
class Account(models.Model):
account = models.IntegerField(primary_key=True, db_column='Account')
description = models.TextField(db_column='Description')
balance = models.DecimalField(decimal_places=4, max_digits=19, db_column='Balance')
debits = models.DecimalField(decimal_places=4, max_digits=19, db_column='Debits')
credits = models.DecimalField(decimal_places=4, max_digits=19, db_column='Credits')
class Meta:
db_table = 'Account'
permissions = (("view_accounts", "Can view accounts"),
)
@staticmethod
def resultSet(startDate, endDate):
try:
cur = connection.cursor()
cacheKey = 'Account_%s_%s' %(startDate, endDate)
cachedSp = cache.get(cacheKey)
print cacheKey
if not cachedSp:
print 'not cached'
cur.execute('database.dbo.AccountsSelect %s, %s', (str(startDate), str(endDate)))
cachedSp = cur.fetchall()
#for row in cachedSp:
#if row[5]!= -1:
#print format(row['debit'],'%4f')
cachedSp = cur.fetchall()
cur.close()
cache.set(cacheKey,cachedSp,settings.CACHE_CUSTOM_APP_SECONDS)
except Exception, e:
print e.args
return [Account(*row) for row in cachedSp]
以下是ODBC/freetds的配置文件。我使用SQL Server 2008.Freetds v1.12,django-pyodbc v0.9和pyodbc v3.0.6。
freetds.conf
[global]
# TDS protocol version
; tds version = 8.0
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
[tes-db-1]
host = tes-db-1.doamin.test.com
port = 1433
tds version = 8.0
ODBC.INI
[default]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = SERVER
Database = DATABASE
odbcinist.ini
[FreeTDS]
Description = TDS Driver (Sybase/MS-SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 1
client charset = utf-8
另外,我有我的设置文件(INSTALLED_APPS)和工程应用同步正确,我在我的所有应用程序中使用缓存,我不认为这是问题的根源,如果这应该很重要。
除此之外四舍五入问题,一切工作正常,任何帮助表示赞赏。
什么版本pyodbc和SQL Server驱动程序? – Bryan 2013-04-25 15:25:17
我使用freetds v 1.12(通过apt-get安装)。 pyodbc v3.0.6和django-pyodbc v0.9(均通过pip)。 SQL Server 2008. Ubuntu 12.04。感谢您的查询,Beargle – Adel 2013-04-25 18:52:48
事实上,你正在使用freetds的是信息的重要的一点,请更新你的'freetds.conf'文件的副本的问题。 – Bryan 2013-04-26 12:41:20